0

So, I have a loop that works great.

But I need to count how many posts that loop has, and I need to do it via a query to the DB.

My loop looks like:

$adsArg = array(
    'offset'             => $offset,
    'post_type'          => 'ads',
    'meta_key'           => $metaKey,
    'tax_query'          => $taxQuery,
    'meta_query'         => $metaValue,
);
$adsQuery = new WP_Query($adsArg);

And the query I have so far looks like:

$wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM " . $table_name . " WHERE post_type = 'ads' AND post_status = 'publish' AND tax_query = " . $taxQuery . " AND meta_query = " . $metaValue . ", array() ) );

It's not working and I have no idea the proper way to do that.

The counter has to be done this way, with a direct query to the database.

  • what's the context? why would you do the same query twice? – David Sword Mar 14 '18 at 18:11
  • Hey David. I have to, because for some reason the post_count is not working. I'm gonna try your solution tomorrow and see if it works. Thnx in advance. – Marcelo Henriques Cortez Mar 14 '18 at 22:52
  • 1
    post_count should work, you should be trying to fix that problem not come up with a complicated workaround like querying the database twice. What isn't working about post_count? – Jacob Peattie Mar 15 '18 at 0:59
  • It just doesn't count anything. The problem is, for this project, I can't touch the loop. The other guy did it, it is working, the posts are all retrieved. I have no idea if there is something else messing with the code before I use the post_count. But since I have to implement a small verification, I thought it would be faster to do that way. And the project owner is ok with that. – Marcelo Henriques Cortez Mar 15 '18 at 9:34
  • @JacobPeattie I found the problem and the solution. Thnx for pointing that I should try to fix it. I didn't have to touch the other code, just change the command. I posted an answer. – Marcelo Henriques Cortez Mar 15 '18 at 10:52
2

To get the number of posts you can do it easily without a second query with

$adsQuery->post_count;

But if there's some reason (I'd love to know the context!) I can't grasp that the same query has to be run twice and to the database directly- to answer your question as to why it's not working:

  • Has $table_name been defined? Are you using {$wpdb->prefix}_posts or {$wpdb->posts} in $table_name?

  • You have no sql string value quotes around $taxQuery or $metaValue values, so that'll break the query, unless they integers

  • Has $wpdb has been globally declared?

  • Are you using $wpdb->print_error() to show the issue?

  • If you echo'ed the string query instead of running it, do it look like you expect? whats missing? if you paste it in phpmyAdmin/sqlworkbench does it give you any indication of the problem?

  • do you have any errors in your servers errors logs? mysql error logs?


I would suggest something like the following

<?php
global $wpdb;

$query = "
    SELECT COUNT(ID) 
    FROM   {$wpdb->posts}
    WHERE  post_type = 'ads' 
           AND post_status = 'publish' 
           AND tax_query = \"%s\"
           AND meta_query = \"%s\"";

// show query
// echo "<pre>{$query}</pre>";

$count = $wpdb->get_var( $wpdb->prepare( 
    $query, 
    $taxQuery,
    $metaValue
));



echo "<pre>";
if($wpdb->last_error !== '') {
   echo "🔥 ERROR: ";
   $wpdb->print_error();
} else {
   echo "Count: {$count}";
}
echo "</pre>";
  • Hey David, thnx for the answer. Your code worked, but since you talked about the post_count and Jacob pointed that I should make post_count work... I searched and found taht post_count gets the posts found in the query including the 'post per page'. But I need to get all the posts inside the query, so I had to use found_posts. I'm gonna accept your answer, since my question didn't explained that little detail. And I'm gonna post my solution as an answer also. – Marcelo Henriques Cortez Mar 15 '18 at 10:41
1

Since I left out a detail in my question, I approved David Sword's answer because it helped me connect correctly to my DB the way I asked.

But in my specific case, the 'post_count' didn't work because it considers the 'offset' and 'post per page' attr.

Since I need all the posts in the query, I had to use 'found_posts'.

Ex.: $adsQuery->found_posts;

This hook is especially useful when developing custom pagination. For instance, if you are declaring a custom offset value in your queries, WordPress will NOT deduct the offset from the the $wp_query->found_posts parameter (for example, if you have 45 usable posts after an offset of 10, WordPress will ignore the offset and still give found_posts a value of 55).

https://codex.wordpress.org/Plugin_API/Filter_Reference/found_posts

https://codex.wordpress.org/Class_Reference/WP_Query

Thnx also to @JacobPeattie for pointing that I should be trying to fix 'post_count' problem.

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.