I have a very strange problem that I am trying to troubleshoot:

I have a query (to build an array) that I run on a custom post type inside my plugin. It looks like this:

function makePointerQuery(){
//global $post; 
$pqargs = array(
    'post_type' => 'sbap_pointer',
    'posts_per_page' => -1
$pointer_query = array();
$pp_query = get_posts($pqargs);
if ($pp_query) :
foreach ( $pp_query as $post ) : setup_postdata( $post );
       $pointer_query[] = array(
            'id'       => get_post_meta($post->ID, '_sbap_pointerid_text', true ),
            'screen'   => get_post_meta($post->ID, '_sbap_screen_text', true ),
            'title'    => get_the_title($post->ID),
            'content'  => get_the_content($post->ID)

endforeach; endif;
return $pointer_query;

If I leave out global $post, my plugin seems to function just fine, but I get the following PHP Notice (with debugging on):

Notice: Trying to get property of non-object in /wp-includes/post-template.php on line 289

BUT if I include global $post, while that notice goes away, something very strange happens. Every ("Add New") new post page ("post-new.php" in posts, pages, my custom post type, etc) is pulling up and filling out the data from my first custom post (ie the title, content, slug are all filled in with that post's data instead of the blank fields as would be normal for a new post).

I am at a loss as to how to eliminate that error notice while still having my plugin function as expected (without messing up every new post in admin).

Any ideas? Thanks in advance!

1 Answer 1


You have two problems in your code.

  1. You use $post as a iteration variable which is an habit best avoided
  2. You use setuppostdata which changes the global $post without you using any function that actually needs it

your loop should look like

foreach ( $pp_query as $p) : 
       $pointer_query[] = array(
            'id'       => get_post_meta($p->ID, '_sbap_pointerid_text', true ),
            'screen'   => get_post_meta($p->ID, '_sbap_screen_text', true ),
            'title'    => get_the_title($p->ID),
            'content'  => get_the_content($p->ID)

endforeach; endif;

then the reset_post_data is unneeded as well

  • Thanks for the response, but I had already tried something similar, and using this I still get the PHP notice above about trying to get the property of a non-object.
    – Stephen
    Commented Apr 6, 2016 at 14:40
  • Well, here is something very strange: As I said, with the technique above I was STILL getting the PHP notice. So I started looking line by line to see if any of them could be written differently, and simply by replacing "get_the_content($p->ID)" with "$p->post_content", the problem went away!. I will mark your answer as the solution since it led me to that anyway, thanks.
    – Stephen
    Commented Apr 6, 2016 at 14:59
  • Probably the reason is that get_the_content expects totally different parameters, and for it you actually do need the global $post to be set, but the way you ended up doing it is better IMO Commented Apr 6, 2016 at 15:03

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.