Terms 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.
Related Posts:
- Is it a good idea to improve meta query performance by adding tax query?
- Return only Count from a wp_query request?
- WordPress tax_query “and” operator not functioning as desired
- Conditional arguments for WP_Query and tax_query depending on if $somevar has a value
- Does tax_query really beats meta_query in all situations?
- Count the number of images uploded on the website
- Is it true $wpdb->get_results is faster than WP_Query in most cases?
- Slow SQL_CALC_FOUND_ROWS Query
- get_the_title($postID) OR get_the_title()?
- if wp_query taxonomy term have posts
- WooCommerce: filter by parent product’s taxonomy and product variation’s meta data
- How to combine tax_query and date_query in WordPress
- Querying by taxonomy vs Querying by Custom fields Speed Comparison
- Using tax_query reverses my post_type argument in a custom WP_Query
- WP Query for Posts (Products) in Specific Category that has 2 Specific Tags (*AND* both tags not *OR*)
- Slow speed and high CPU usage for my code. How to improve it?
- Multiple queries on Home Page – page load/site speed issue
- Get term by custom term meta and taxonomy
- WP_Query Performance Issues with meta_query
- tax_query shows no results if nothing is selected
- WP_Tax_Query with post_tag not working
- get complex results set according to category structure
- WP_Query tax_query problem
- Count total number of post in foreach loop
- Too many posts cause slow load when paginating
- tax_query OR with empty result returns all posts
- Empty tax_query array returns an empty array
- WP_Query times out
- Get list of terms that have posts in another term
- Improve wp query perfomance on large db
- tax_query: Order by slug?
- reducing the amount of wp_query calls
- Tax query clause inside a meta query clause?
- Querying on multiple taxonomies pulled from $_GET checkbox array not working?
- Getting the current post number / make posts increment
- How to use wp_query to post count by search term > group post count by date > convert to json. (ex. result 12-21-2020 – 343)
- WP_Query | Tax_Query Relation | Unable to use ‘OR’ as it then allows all products, help me finish my query?
- Slow page loads due to WordPress Core Query
- Taxonomy Query Relation field not behaving correctly?
- Display tags with random thumbnail from selection of posts with that tag
- Should wp_query automatic meta and term cache priming be used in an enviroment with an object caching
- Loop Performance > JSON files vs. WP_Query vs. REST API
- use of 10 new WP_Query : loading too slow (with url) [closed]
- Query with meta_query and tax_query together not working properly
- Adding a tax_query to a WP_Query Object
- Finding WordPress Posts assigned to multiple categories
- Order or Orderby in tax_query (How to define order of terms in WP_Query)
- What is the best way (regarding performance) to set transients for logged in users?
- exclude product with available tag
- How to add tax_query to $args with concatenation
- How to print term name inside wp post loop
- Get posts in taxonomy randomly
- How to search for meta_query LIKE or tax_query LIKE and grab these posts on search results?
- Is it possible to add relation between meta_query and tax_query?
- 2 queries with counters
- How to avoid filling up an array each time I run a WP_query?
- Tax_Query using WP_Query not working
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Which is from this queries is more faster
- Having a variable based on $post_count increase when i move to the next page of results
- Any quicker alternative for WP_Query “NOT IN”
- Tax Query on product_cat using NOT IN as operator does not exclude that category
- ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)
- Facing problem with tax_query results
- Counting instances of words in the results of a post query
- Query posts by post type, author and post meta
- order posts by a secondary query that counts items
- wp_query with meta_query and tax_query
- list all post who have mutual taxonomy as current taxonomy!
- Unusual high query of user meta data
- WordPress post_status and meta_query
- WP_Term_Query->get_terms() very slow query for WordPress filter
- Tax_Query not working – connecting to second wordpress database
- WordPress tax_query ignoring relation OR
- Optimal way to make tags in tax_query optional?
- Showing the correct number of posts_per_page when avoiding the use of post__not_in
- in tax_query this Is the code correct? I need to access beginner video posts for different subject
- display all posts from category with and without terms in chronological order
- In Product Category archives how to show Posts having same/similar prod_cat slug structure?
- Pull posts from all categories if quantity is not met?
- How can I get all the posts that are related with a specific taxonomy term?
- How to combine nested tax_query logic with other nested query logic?
- Custom taxonomy and query multi conditions
- tax_query not working for taxonomy slug
- Extending WP Query: Custom geolocation meta values work, but tax_query breaks
- Empty ‘terms’ in ‘tax_query’ returns an empty array
- Custom query for tag and custom tag from 2 post type
- WP_Query: Meta_Query with serialized value (or a workaround)
- Multiple tax_queries: display posts titles from several custom taxonomie
- Website goes slow down after importing long database
- Escaping WP_Query tax_query when term has special character(s)
- How to get hierarchical number of custom taxanomy
- Display Count of posts
- Optimising amount of calls to custom fields
- Get the tax term in which is a post via wp_query
- Post OrderBy slow performance
- Output ACF field dynamicaly within a taxonomy loop [closed]
- WP_Query with tax_query not working
- Custom query for certain post type OR another post type with a certain category
- Custom query with custom filtering returning incorrect results