0

I'm wondering how I can request the amount of posts by passing certain arguments (you could see this as part of a "query"). See the example below.

$args = [
    "numberposts" => -1, 
    "post_type" => "jobs", 
    "s" => "developer", 
    "tax_query" => ["taxonomy" => "IT", "field" => "slug"]
];
$posts = get_posts($args);
$count = count($posts);

I'm aware of the fact that the following function works perfectly for my case. However, it's extremely exhausting to execute. Instead of actually getting all posts within a query, I'm just looking for a function that just counts the posts with certain arguments.

Please bare in mind that I'm passing on certain meta_queries and tax_queries which will make some queries to the database fairly hard on itself already.

Hope someone can help.

2

The easiest way to optimize this query would be to add 'fields' => 'ids' to the arguments. This way only ids of posts will be retrieved from DB (and usually that’s a big change).

$args = [
    'numberposts' => -1, 
    'post_type' => 'jobs', 
    's' => 'developer', 
    'tax_query' => ['taxonomy' => 'IT', 'field' => 'slug'],
    'fields' => 'ids'
];
$posts = get_posts($args);
$count = count($posts);

On the other hand you can use WP_Query instead of get_posts. This way you can set posts_per_page to 1, so only one ID will be retrieved and use found_posts field of WP_Query to get the total number of posts matching your criteria.

$args = [
    'posts_per_page' => 1,
    'post_type' => 'jobs', 
    's' => 'developer', 
    'tax_query' => ['taxonomy' => 'IT', 'field' => 'slug'],
    'fields' => 'ids'
];
$query = new WP_Query($args);
$count = $query->found_posts;
  • I worked around your method and it already improved the script a lot. Will test in full context asap :-) – Jim Feb 5 '19 at 23:03
  • @Jim and what are the results of your tests? – Krzysiek Dróżdż Feb 6 '19 at 16:27
  • Will keep you posted :-) Migrating to live version as I speak – Jim Feb 7 '19 at 11:27
  • Speed has improved but it's still a pretty large request to be pulled from the database I think... The script first loops through all taxonomy terms and then checks the amount of posts linked to this taxonomy term (which is pretty heavy because the term has to be passed within the WP_Query in order to get the amount). The post type in which the query is being made exists of +/- 10.000 posts, so I think this is the max. Thanks for your help ;-) – Jim Feb 8 '19 at 13:06
  • @Jim if you'll use the second solution (with WP_Query), then only one record will be obtained from DB. I'm not sure if I get you right - there should be no looping through any taxonomies - unless u do this to build the query and run this query many times - but I would use a raw SQL in such case.... – Krzysiek Dróżdż Feb 8 '19 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.