Wordpress Notice: Trying to get property of non-object

user1609391 picture user1609391 · Feb 18, 2013 · Viewed 18.2k times · Source

I have a custom post type set up on my blog and a special page for the taxonomy. On the taxonomy page I am getting the below error. Can anyone give me some tips on how to resolve this error?

The page loads fine and works as I would expect. But I get the below error if I have debug set to true. I would like to resolve this. I pasted the cost from the loop which is run two time on the page with different criteria.

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

Code:

<?php
query_single('dealers', 'publish', '1', $taxtype, $value);
 ?>

 <?php if (have_posts()) : ?>
 <?php while (have_posts()) : the_post(); ?>

 <?php 
   $address=get_post_meta($post->ID, 'wpcf-street_address', TRUE); 
   $city=get_post_meta($post->ID, 'wpcf-city', TRUE); 
   $state=get_post_meta($post->ID, 'wpcf-state_abbreviation', TRUE); 
   $zip=get_post_meta($post->ID, 'wpcf-zip_code', TRUE); 
   $phone=get_post_meta($post->ID, 'wpcf-phone_number', TRUE); 
   $paid=get_post_meta($post->ID, 'wpcf-paid', TRUE);
   $post_id=get_the_ID();
   get_each_dealer_brand($post_id);?>

  <?php 
  echo "<ul class=\"ullisting\">";
  if($paid==1)
  {
   echo "<li><p class=\"plisting\"><strong><a href=\"";the_permalink(); echo  "\">";the_title();echo "</a></strong></p></li>";
    echo "<li><p class=\"plisting\">$address | $city, $state $zip</p></li>";
echo "<li><p class=\"plisting\">P: $phone</p></li>";
    echo "<li><p class=\"listing\"><span><small>$brands_list</small></span></p></li>";
 }
echo "</ul>";
?>

 <?php endwhile; ?>

 <?php
 wp_reset_query(); 
 wp_reset_postdata(); 
 unset($brands_list);
 ?>

This is the function referenced above:

   function query_single($posttype, $poststatus, $paidvalue, $taxtype, $value) {

     global $wp_query;
    $wp_query = new WP_Query();
  $args = array(
   'post_type' => $posttype,
   'post_status' => array($poststatus),
   'orderby' => 'rand', 
   'posts_per_page' => 20,
   'meta_query' => array(
       array(
           'key' => 'wpcf-paid',
           'value' => array($paidvalue),
           'compare' => 'IN',
       )
   ),
    'tax_query' => array(
            array(
                'taxonomy' => $taxtype,
                'field' => 'slug',
                'terms' => $value
            )
        )
    );
   return $wp_query->query($args);
   }

Answer

Vinith picture Vinith · Jun 26, 2014

This error will arise when you try to access posts inside your theme,

in page-template.php we have,

function get_the_ID() {
    return get_post()->ID;
}

Whenever we are accessing posts we need to check the below condition and make sure it works as default, because we may not use wp_reset_postdata(); all the time so,

  global $post;    
//check if post is object otherwise you're not in singular post
  if( !is_object($post) ) 
     return;
//If Object
  $somevariable = get_post_meta(get_the_ID(), $something->get_the_id(), TRUE); 

Hope this helps. Thanks.