1

I have a strange problem I'm facing when using WP_Query with multisite in a plugin I'm writing which hooks into add_meta_boxes. I'm wanting to display all post titles in a dropdown in a meta box.

While adding or editing a post in blog no. 2 (it could be any blog no.), I am looping through blog no. 1 and getting post titles from a custom post type to populate in a select dropdown. This works as it should, but in the process, the text in my excerpt is been replaced with the excerpt in the last post of the loop.

I have disabled all plugins. I have enabled 2015 theme on all sites. I'm using wordpress 4.1. I have tried with just posts instead of custom posts and the problem still exists. Below is the code I am using:

switch_to_blog( 1 ); 
$qry_args = array(
    'post_status' => 'publish',     
    'post_type' => $typenow,        
    'posts_per_page' => -1,         
    );

$blog_query = new WP_Query( $qry_args );

if ( $blog_query->have_posts() ) {  
    while ( $blog_query->have_posts() ) {
        $blog_query->the_post();

        // get_the_ID(); and put in in an <option></option>                 
    }
} 
wp_reset_postdata();
restore_current_blog();     

I have narrowed the source of the problem down to the line $blog_query->the_post();

Can anybody point out why my excerpt in a post in blog 2 is being replaced with an excerpt from a post in blog 1 while looping through blog 1? I'm rather new to writing plugins, so perhaps I'm doing something wrong that I know nothing about.

Thank you for your help,

Adrian

EDIT:

I just done some more tests, and found i was getting the following php warning with the plugin Query Monitor:

PHP error:
Trying to get property of non-object

Count:
6

Location:
wp-includes/capabilities.php:1203

Call Stack:
map_meta_cap()
WP_User->has_cap()
current_user_can() meta_form()
post_custom_meta_box()
do_meta_boxes()

When I place the above code in admin_footer instead of add_meta_boxes the problem is fixed. So it appears to be possibly a timing issue?? If so, how can I query my posts in add_meta_boxes?

1
  • do you have your code hooked in an action? Commented Jul 21, 2015 at 1:47

2 Answers 2

0

Check to see what you're getting. The new WP_Query() isn't working for some reason. Do a var_dumpon it. I suspect that you aren't getting any results from the query, resulting in a Trying to get property of non-object when you try to access the methods.

2
  • Hi Caleb, thank you for your answer. I'm no expert on wordpress, but I'm not sure if what you said is the cause of my problem. Wp_Query() works as I do get my post titles, it is just that the last post in the query is effecting my excerpt in the current post I'm editing when it is executed from the hook add_meta_boxes. I did a var_dump on the new WP_Query() as you suggested, and I don't see anything out of the ordinary. I have 70 posts and they are all listed.
    – Adrian
    Commented Dec 28, 2014 at 22:43
  • I'm no wordpress expert either! Commented Dec 29, 2014 at 0:24
0

You reset the post data before switching the blog back to the original. This means that the global post related variables will be set to something unexpected on blog 1 because you are still in its context.

The correct order should be

restore_current_blog();
wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.