在循环中使用新的WP_QUERY

时间:2021-09-15 作者:Jay Loftus

我正在尝试查询数据库以统计文件下载的日志条目,然后在列出搜索结果时显示缩略图。

来自搜索。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));
`如果我将$loveCount更改为一个固定数字并注释掉该查询,该功能将根据需要在前端显示该数字,因此,即使该查询在网站的其他地方也可以使用,但该查询似乎没有运行。

有人能帮我理解为什么这个查询不起作用,或者帮我解决问题吗?

非常感谢。

var\\u转储输出:

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 "

1 个回复
SO网友:Jay Loftus

以下代码似乎有故障:

$query_args[\'tax_query\'] = array(
        array(
          \'taxonomy\'  => \'edd_log_type\',
          \'field\'     => \'slug\',
          \'terms\'     => \'file_download\',
        )
      );
它似乎将tax\\u query参数与搜索查询本身的参数相结合,通过删除此参数,我得到了更好的结果,尽管它计算的是所有日志,而不仅仅是那些具有“file\\u download”id的日志。

对我来说,下载量是成百上千,有些帖子可能还有3-5条其他日志。

修复了一半,但现在还可以使用,除非任何人都能想出一种方法让查询识别一组单独的税务参数?

谢谢大家!:)