Counting the posts of a loop (WP_Query)?

I tried this way to display NO of post:

<?php 
 $news_2 = new WP_Query( array ('post_type'=> 'jobs','posts_per_page'=> '10' , 'meta_key' => 'status_for_jobs','meta_value' => '1') );
  if ( $news_2->have_posts() ) { while ( $news_2->have_posts() ) { $news_2->the_post();

  $count = $news_2->post_count;

  ?>

    <li><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></li>



    <?php } } ?> <?php wp_reset_query(); ?> 

if the NO of post = 0 i need to display this :-

Read More
<?php 
 $news_2 = new WP_Query( array ('post_type'=> 'jobs','posts_per_page'=> '10' , 'meta_key' => 'status_for_jobs','meta_value' => '1') );
  if ( $news_2->have_posts() ) { while ( $news_2->have_posts() ) { $news_2->the_post();

  $count = $news_2->post_count;

  if ($count  == '0') {

  ?>
  <li><h3><a href="javascript:void(0)">No Post</a></h3></li>

  <?php
  } else {
  ?> 
    <li><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></li>

<?php  }  ?>

    <?php } } ?> <?php wp_reset_query(); ?> 

But instead of the total of posts, I not getting any thing.

Any suggestions to fix this?

Related posts

Leave a Reply

2 comments

  1. Some additional information, no need to count the posts again, because WP_Query already did that for you. To clarify this, some information from the Class Reference of WP_Query as found in the »Properties« section:

    $post_count
    The number of posts being displayed.

    $found_posts
    The total number of posts found matching the current query parameters

    What this means is

    1. $post_count won’t give you the total post count. It will most likely give you the number of posts you’ve defined with the post_per_page parameter, unless you’ve fewer posts than that or you’re on the last page and there are only fewer posts left.
    2. $found_posts can be used to get the total number of post related to a specific query. So there is no need to count them again.

    In your case you can get the total count into your $count variable like this:

    $count = $news_2->found_posts;
    

    Besides of that @helgatheviking is right that, from what you’ve shown in your question, you don’t need a extra conditional, but can just use the have_posts() method, in the conditional you already have, for that, like she suggested.

  2. You don’t need to count the posts to show something different if no posts are found. You can just use the else part of your if($news_2->have_posts()) check.

    $news_2 = new WP_Query( array ('post_type'=> 'jobs','posts_per_page'=> '10' , 'meta_key' => 'status_for_jobs','meta_value' => '1') );
    
    if ( $news_2->have_posts() ) { 
    
        while ( $news_2->have_posts() ) { 
    
            $news_2->the_post();
    
        ?> 
    
        <li><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></li>
    
    <?php  } 
    
        } else { ?>
    
        <li><h3>No Post</h3></li>
    
        <?php } ?> 
    
    <?php wp_reset_query(); ?> 
    

    But if you truly need to count the number of posts returned, you can use

    $count = count( $news_2->posts );