I’m trying to count the number of total posts of a custom post type “jobs”. My query just returns “0” when I know there are posts. I don’t think it is checking that the post type has posts, but I’m clueless as to why… any ideas?
<?php $jobs = new WP_Query(array( 'post_type' => 'jobs' ));?>
<?php if ($jobs->have_posts()) {
$count_posts = wp_count_posts()->publish;
if ( $count_posts == "1" ) {
echo "<h2>There is currently one vacancy...</h2>"; }
else { echo "<h2>There are currently $count_posts vacancies...</h2>"; }
} else { ?>
<h2>There are currently no vacancies.</h2>
<?php } ?>
The
wp_count_posts
function has parameter$type
for post type to count, you should use this parameter if you want to get number of jobslike so:
Replace these with your meta_key and meta_value:
Another way, using WP-CLI: