最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - How to count posts with specific arguments

matteradmin9PV0评论

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.

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.

Share Improve this question edited Feb 5, 2019 at 17:16 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 5, 2019 at 16:08 JimJim 1601 silver badge12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 6

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;
Post a comment

comment list (0)

  1. No comments so far