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 "
fieldsasidsthe query returns an array of IDs, not a query object. So$logs->post_countwon't work. Trycount( $logs ).$logs->post_countdoes work. It's just that$logs->postswill be an array of post IDs instead of objects.var_dump( $logs->request );- and share the dump output, or just check for yourself whether the query is good.get_the_ID()because it's an archive page. so,'post_parent' => get_the_ID()won't work. What's the var_dump from a page where it works? We'll want both to compare & find where they're different