How could I get the static total number of posts which wouldn’t change
irrespective of what the user filters?
You may be looking for wp_count_posts()
: Codex: WP COUNT POSTS
Example getting number of all published posts:
function get_all_them_posts(){
$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;
return $published_posts;
}
In template:
<?php echo get_all_them_posts(); ?>
For Custom Post Type:
Functions.php:
function get_all_them_cpt_posts(){
$post_type="your_post_type_slug_here";
$count_posts = wp_count_posts( $post_type );
$published_posts = $count_posts->publish;
return $published_posts;
}
In template:
<?php echo get_all_them_cpt_posts(); ?>
As noted by Sam, and in this older WPSE answer, $found_posts
is in reference to what the query is holding. $post_count
is in reference to what is being displayed (often the number set in the posts_per_page parameter). I think wp_count_posts()
is what you’re looking for though.
For Your Updated Code
(CPT version above)
Okay, it would be better to add the first code block to the functions.php of your theme (or child theme if you are using one). This:
function get_all_them_posts(){
$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;
return $published_posts;
}
Then where you wish to have the number of total posts in the template, replace:
<?php echo $query->found_posts; ?>
With:
<?php echo get_all_them_posts(); ?>
That line will call the function added to the functions.php
By doing it that way, you can use it in other template files without having to rewrite that function every time.
I hope that helps!