Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created search.php from Twenty Eleven theme search template. Search template works fine but i am having a problem when no results are found.

I am getting this PHP Notice: Notice: Trying to get property of non-object wp-includes/post-template.php on line 29

Post-template line 29 looks like this:

function get_the_ID() {
return get_post()->ID;<-this is the source of the problem 
}

I am assuming that this happens because get_post()->ID is not set (when no results are displayed) because inside my search template i have put this code:

<?php if ( !isset ($post->ID)) {
echo 'not set';<- this will echo "not set" if NO results are found and 
               PHP notice will be displayed:(               
}
else{
echo 'set';<- this will echo "set" if results ARE found and 
               PHP notice will NOT be displayed  
}
?>

So my question is how to SET $post->ID (if it is not set) in order to remove PHP Notice?
Any help is massively appreciated Thank You!!

Search php code:

<?php

 get_header(); ?>
 <?php get_sidebar(); ?>
 <?php get_sidebar('secondary'); ?>
 <?php if ( !isset ($post->ID )) {
  echo 'not set';
   }
   else{
   echo 'set';
   }
   ?>
<div id="blogwrapper">
<div id="blog">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s' ,'your-override' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

    <div <?php post_class();?> id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>


   <div class="entry">  
                <div class="featured_img">
                <?php the_post_thumbnail();
      echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';?>
     </div><!--/featured_img-->
        <?php  // let's enable more link on pages...
        global $more;
        $more = 0;
        ?>
            <?php the_content(); ?> 
            <div class="clear"></div>               
            <div class="custom_fields"><?php the_meta(); ?></div><br/>
            <p class="postmetadata">

            <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br/><?php the_tags('Tags:', ', ', '<br />'); ?>
            <?php _e('Posted on:&nbsp;'); ?><?php the_time('l, F jS, Y'); ?><br/>               
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>
    </div>
 <?php endwhile; ?>
 <div class="navigation">
    <?php
    global $wp_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, $paged ),
    'total' => $wp_query->max_num_pages
    ) );
     ?>
    </div>
<?php else : ?>
        <article id="post-0" class="post no-results not-found">
            <header class="entry-header">
                <h1 class="entry-title"><?php _e( 'Nothing Found'); ?></h1>
            </header>
            <div class="entry-content">
                <p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.'); ?></p>
                <?php get_search_form(); ?>
            </div><!-- .entry-content -->
        </article><!-- #post-0 -->      
        <?php endif; ?>
        </div>
        </div><!--/blogwrapper-->   
        <?php get_footer(); ?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

get_post() returns the post object to the loop. So when you look for its ->ID if no object is there you try to make php to look for ID of nothing, and it gives you the notice. Change post->ID for

    if( ! get_post() )
share|improve this answer
    
Up voted and accepted thx @Svetlana Silina! –  Dejo Dekic Jun 6 '13 at 14:53

Well looks like that my problem wasn't search.php but my footer.php this code caused an error <?php post_comments_feed_link( $link_text = 'Comments RSS', $post_id = 'post_id', $feed = 'rss2' ); ?> anyway my footer code looked like this :

<div id="footer">
 <?php wp_footer(); ?> 
<p>
<strong>Copyright 2013 <?php bloginfo('name'); ?> | All Rights Reserved.</strong>
Designed by Dex
</p>
<p><a href="<?php bloginfo('rss2_url'); ?>">Latest Stories RSS</a> | 
<?php post_comments_feed_link( $link_text = 'Comments RSS', $post_id = 'post_id', $feed = 'rss2' ); ?></p>  
</div>
</div><!--/wrapper-->

And I have modified it using @Svetlana Silina answer to this:

<div id="footer">
<?php wp_footer(); ?> 
<p>
<strong>Copyright 2013 <?php bloginfo('name'); ?> | All Rights Reserved.</strong>
Designed by Dex
</p>
<p><a href="<?php bloginfo('rss2_url'); ?>">Latest Stories RSS</a> |    
<?php if(get_post()){
?>
<?php post_comments_feed_link( $link_text = 'Comments RSS', $post_id = 'post_id', $feed = 'rss2' ); ?></p>      
<?php } else if(!get_post()){ 
?>
<?php post_comments_feed_link( $link_text = 'Comments RSS', $post_id = '1', $feed = 'rss2' ); ?></p>    
<?php } ?>
</div>

And all is good now no PHP notice even if no results are shown. Life is good again:=)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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