WP_Query in functions.php overrides global $post object, even with wp_reset_query()
I've written a function that grabs all posts that are drafts and puts them as a dropdown in my toolbar for easy access to complete. This works just fine.
The problem is that it seems to mess with my global $post object, specifically when called in my post/page editor.
So if I'm in my post editor: http://example.com/wp-admin/post.php?post=147action=edit
147 is the post I want to edit. But instead of showing the post corresponding to 147, it's showing the last draft in the loop, even though I'm resetting the query variable with wp_reset_query().
Thanks!
Here's my function:
// 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();
}
Topic wp-reset-query functions posts Wordpress
Category Web