我编写了一个函数,它可以抓取所有草稿帖子,并将它们作为下拉菜单放在我的工具栏中,以便轻松访问完成。这很好用。
问题是它似乎弄乱了我的全局$post对象,特别是在我的帖子/页面编辑器中调用时。
因此,如果我在我的帖子编辑器中:http://example.com/wp-admin/post.php?post=147&action=edit
147是我想编辑的帖子。但它并没有显示对应于147的帖子,而是显示了循环中的最后一个草稿,尽管我正在用wp\\u reset\\u query()重置查询变量。
谢谢
以下是我的功能:
// Get Company Drafts
add_action( \'admin_bar_menu\', \'add_nodes_and_groups_to_toolbar\', 1000 );
function add_nodes_and_groups_to_toolbar( $wp_admin_bar ) {
$user_id = bp_loggedin_user_id();
$draft_query = new WP_Query(array(
\'connected_type\' => \'companies_to_users\',
\'connected_items\' => $user_id,
\'nopaging\' => true,
\'post_status\' => \'draft\'
));
if ($draft_query->have_posts()) {
// Add \'Drafts\' to the Toolbar
$args = array(
\'id\' => \'drafts\',
\'title\' => \'Drafts\',
);
$wp_admin_bar->add_node( $args );
// Add the category \'Companies\' as the sub-menu of Drafts
$args = array(
\'id\' => \'drafts_company\',
\'title\' => \'Companies\',
\'parent\' => \'drafts\'
);
$wp_admin_bar->add_node( $args );
while($draft_query->have_posts()) : $draft_query->the_post();
// Add the post title as the sub-menu of the category sub-menu
$args = array(
\'id\' => $post->ID,
\'parent\' => \'drafts_company\',
\'title\' => get_the_title(),
\'href\' => apply_filters(\'gform_update_post/edit_url\', $post->ID, home_url(\'/edit-company/\'))
);
$wp_admin_bar->add_node( $args );
endwhile;
}
wp_reset_query();
}