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="https://wordpress.stackexchange.com/questions/139614/<?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 :-
<?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="https://wordpress.stackexchange.com/questions/139614/javascript:void(0)">No Post</a></h3></li>
<?php
} else {
?>
<li><h3><a href="https://wordpress.stackexchange.com/questions/139614/<?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?
2 s
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
$post_count
won’t give you the total post count. It will most likely give you the number of posts you’ve defined with thepost_per_page
parameter, unless you’ve fewer posts than that or you’re on the last page and there are only fewer posts left.$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.