0

I would like to get just the number of posts published in one particular day. I'm using this code, but this retrieve lot of code:

$day = date('Ymd');
$query01 = array('numberposts' => -1, 'post_type' => array('post', 'video', 'image', 'review'), 'm' => $day);
$mypost = get_posts($query01);
$counter = count($mypost);
wp_reset_postdata();

Is there a lighter way to obtain the same?

2

$wp_query->found_posts can be used to get the number of posts. But here are arguments you can use to optimize the query and avoid useless SQL retrievals:

cache_results' => false, // Turns off post caching
'no_found_rows' => true, // To optimize when pagination is not required
'fields' => 'ids'  // To only retrieve IDs information, nothing else (avoid collecting all useless post data)

If you have more optimizing tips for simple queries, I would be happy to hear about them

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.