1

I just want to show the total number of custom posts matching some conditions (arguments).

Do I have to make the entire query (get_posts, query_posts...) and get found_posts from it or is there a more efficient way to query the totals?

For instance (using an events plugin),

$args = array(
                'posts_per_page' => -1,
                'post_type' => 'event',
                'suppress_filters' => false,
                'showpastevents' => true,
                'post_status' => 'published',
                'group_events_by' => 'series',
                'meta_query' => array(
                                    array(
                                    'key' => '_event_public',
                                    'value' => '1',
                                    )
                                )
);
$q_posts = get_posts($args);
$alltime_events = count($q_posts);

is giving me 1344 events. Is that not an inefficient query?

EDIT:

I added 'fields' => 'ids' to the query arguments to only retrieve the ids instead of the whole post object, but I am just guessing it might save some work.

  • You could use SQL directly with $wpdb, but you're not going to get much more help than that without specifying exactly what you're querying. – Jacob Peattie Mar 5 '18 at 13:01
  • @JacobPeattie thanks, i edited my question to provide more info. – Marc Mar 5 '18 at 13:21
  • 1
    To get the count, the query needs to run. Store the value in a transient and refresh it every few hours. – jdm2112 Mar 5 '18 at 16:19
1

There are three sides for the "efficiency" issue.

From the DB side, most of the time is spent in traversing the DB tables. If you need all the rows, you need the DB to traverse all of them no matter what information will be returned. There is probably a cost associated with holding data in memory, but it should be negligible relative to the traversing cost.

For the communication side, it is obvious that the smaller the information being transferred is, the faster the transfer will be completed. It is very hard to guess what the impact is as it will depend on if the DB is on the same host, and if it is not on the quality of the network. This is something you can and should handle by either using 'fields' => 'ids' as you have done, or by creating your own SQL query that will return only the number. IMO the 'fields' => 'ids' is more in line with how wordpress code is expected to work, and even though it is not optimal it should be the preferred way.

From the wordpress side, if you do not specify some specific set of field to return like 'fields' => 'ids', wordpress by default will generate queries that will prime the taxonomy and meta caches which relate to the returned post, so if you know that you are not interested in that information you should "turn it off". The relevant flags are cache_results that can be use to disable all relevant caching, or the more specific update_post_meta_cache and update_post_term_cache

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.