Wordpress Notice: trying to get property of non-object

I have a custom post type created on my blog and a dedicated taxonomy page. On the taxonomy page, I am getting the following error. Can anyone give me some advice on how to resolve this error?

The page loads fine and works as I expected. But I am getting below error if I have debug set to true. I would like to solve this. I inserted the cost from a loop that runs twice 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 mentioned 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);
   }

      

+3


source to share


2 answers


This error occurs when trying to access messages inside your topic,

in page-template.php

we have,

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

      



Whenever we access messages we need to check the condition below and make sure it works by default because we can't 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. Thank.

+2


source


You can try executing your commands in ACTION (because Wordpress is already loaded normally at this time). eg:



ADD_ACTION('init','your_function');

function your_function(){
    YOUR CODES HEREEEEEEEEEEE............
}

      

0


source







All Articles