0

I'm trying to query the database to count log entries for file downloads, this is then displayed an the thumbnail when listing search results.

From Search.php `

<?php 

//JAYJAY Get the download count for this listing so we can pass it to the template part

$query_args = array(
        'post_parent'      => get_the_ID(),
        'post_type'        => 'edd_log',
        'posts_per_page'   => -1,
        'post_status'      => 'publish',
        'fields'           => 'ids',
      );

    
      $query_args['tax_query'] = array(
        array(
          'taxonomy'  => 'edd_log_type',
          'field'     => 'slug',
          'terms'     => 'file_download',
        )
      );
    
    $logs = new WP_Query( $query_args );

    $loveCount = $logs->post_count;

    get_template_part('includes/loop-search-listings', null, array('tpetLoveCount' => $loveCount));

` If I change $loveCount to a fixed number and comment out the query, the functionality works to show the number as needed on the front end, so it seems that this query is not running even though the same query works elsewhere on the website.

Can anyone help me to understand why this query is not working or help me with a workaround?

Many thanks.

var_dump output:

string(1049) "SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) LEFT JOIN ( wp_term_relationships INNER JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id INNER JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id ) ON wp_posts.ID = wp_term_relationships.object_id WHERE 1=1 AND wp_posts.post_parent = 767254 AND ( wp_term_relationships.term_taxonomy_id IN (10) ) AND wp_posts.post_type = 'edd_log' AND ((wp_posts.post_status = 'publish')) OR ( wp_term_taxonomy.taxonomy IN( 'category', 'post_tag' ) AND wp_terms.name LIKE '%wellbeing%' AND wp_posts.post_author = 1 AND wp_posts.post_status IN( 'publish','private' ) ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC "

6
  • When you set fields as ids the query returns an array of IDs, not a query object. So $logs->post_count won't work. Try count( $logs ). Commented Sep 15, 2021 at 8:56
  • 1
    @JacobPeattie $logs->post_count does work. It's just that $logs->posts will be an array of post IDs instead of objects.
    – Sally CJ
    Commented Sep 15, 2021 at 9:16
  • @JayLoftus try inspecting the SQL query - var_dump( $logs->request ); - and share the dump output, or just check for yourself whether the query is good.
    – Sally CJ
    Commented Sep 15, 2021 at 9:19
  • I've attached a var_dump - thanks for your help
    – Jay Loftus
    Commented Sep 15, 2021 at 11:08
  • @SallyCJ (forgot to tag!)
    – Jay Loftus
    Commented Sep 15, 2021 at 11:14

1 Answer 1

0

It seems that the following code is at fault:

$query_args['tax_query'] = array(
        array(
          'taxonomy'  => 'edd_log_type',
          'field'     => 'slug',
          'terms'     => 'file_download',
        )
      );

It seems to be combining the tax_query args with those from the search query itself, by removing this I am getting a better result, although it is counting ALL logs, not just those with the 'file_download' id.

For me it works as downloads are in the hundreds/thousands and some posts maybe have 3-5 other logs that get counted too.

A half fix, but functional for now unless anyone can figure out a way of getting the query to recognise a separate set of tax args?

Thanks guys! :)

1
  • I don't know what added that OR condition having the AND wp_terms.name LIKE '%wellbeing%', but I would try to find the code which added that and modify the code (or if it's a plugin, then try changing the settings) so that the OR is not added to all WP_Query calls. And actually, if you just want to know the total number of posts for a specific query, you don't have to set posts_per_page to -1 and instead, set it to 1 and then use the $found_posts property (e.g. $logs->found_posts) to get the total number of posts.
    – Sally CJ
    Commented Sep 15, 2021 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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