I know this kind of question is legion but I have read through all related topics and I can't find a satisfying solution.
What I want to achieve is simple: on my taxonomy (author) term archive page I have a list of all terms within that taxonomy from which you can select to switch from one term to another.
I want to display the number of posts only from the post type (several CPT are associated with author).
Simply put I want it to look like this:
<ul>
<li class="cat-item"><a href="example/author/albert-camus">Albert Camus</a> (233)</li>
<li class="cat-item"><a href="example/author/jean-fontaine">Jean de la Fontaine</a> (134)</li>
<li class="cat-item"><a href="example/author/victor-hugo">Victor Hugo</a> (82)</li>
</ul>
But I have hundreds of authors and thousands of posts.
Wether I use get_posts
or WP_query
the page takes forever to load. I understand why. Because it makes hundreds of request to count the number of posts.
Most of the topics on this subject in here use this kind of solution to do that :
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term->term_id
)
)
) );
$count = $the_query->found_posts;
If I do that within the loop to list all terms, it destroys my server.
Is there a way to easily count the number of posts from a specific cpt inside a term that is more performance proof?
I know this kind of question is legion but I have read through all related topics and I can't find a satisfying solution.
What I want to achieve is simple: on my taxonomy (author) term archive page I have a list of all terms within that taxonomy from which you can select to switch from one term to another.
I want to display the number of posts only from the post type (several CPT are associated with author).
Simply put I want it to look like this:
<ul>
<li class="cat-item"><a href="example.com/author/albert-camus">Albert Camus</a> (233)</li>
<li class="cat-item"><a href="example.com/author/jean-fontaine">Jean de la Fontaine</a> (134)</li>
<li class="cat-item"><a href="example.com/author/victor-hugo">Victor Hugo</a> (82)</li>
</ul>
But I have hundreds of authors and thousands of posts.
Wether I use get_posts
or WP_query
the page takes forever to load. I understand why. Because it makes hundreds of request to count the number of posts.
Most of the topics on this subject in here use this kind of solution to do that :
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term->term_id
)
)
) );
$count = $the_query->found_posts;
If I do that within the loop to list all terms, it destroys my server.
Is there a way to easily count the number of posts from a specific cpt inside a term that is more performance proof?
Share Improve this question asked Mar 15, 2022 at 18:24 Julien GJulien G 536 bronze badges1 Answer
Reset to default 2Terms generally should have a count
parameter that you can use. I'm not sure if this is accurate across multiple post types though.
This may not be the fastest solution but one option could be the additional parameters for WP_Query to both reduce the number of queries and the returned values. 10up has a good article on this in regards to WP_Query Performance. An example query could look like this:
$the_query = new WP_Query( array(
'post_type' => 'post',
'no_found_rows' => true,
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term->term_id
) ),
'no_found_rows' => true, // Don't calculate pagination
'fields' => 'ids',
) );
You could cache the results until the next time terms are added too using the added_term_relationship
hook which you could take advantage of to speed up future requests.