Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress WP\u查询:过滤tax\u查询或meta\u查询_Wordpress - Fatal编程技术网

Wordpress WP\u查询:过滤tax\u查询或meta\u查询

Wordpress WP\u查询:过滤tax\u查询或meta\u查询,wordpress,Wordpress,如何组合tax_查询和meta_查询的结果 例: 税务查询结果发送至: 邮政1,邮政2 和元查询结果,以: Post3 我想合并这两个查询的结果。 这是我的部分代码: 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'tax1', 'field' => 'term_id', 'terms' => arr

如何组合tax_查询和meta_查询的结果

例: 税务查询结果发送至: 邮政1,邮政2

和元查询结果,以: Post3

我想合并这两个查询的结果。 这是我的部分代码:

'tax_query' => array(
     'relation' => 'AND',
     array(
         'taxonomy' => 'tax1',
         'field'    => 'term_id',
         'terms'    => array(1,2,3),
     ),
     array(
         'taxonomy' => 'tax2',
         'field'    => 'term_id',
         'terms'    => array(1,2,3),
     ),
 ),
 'meta_query' => array(
     array(
         'key'     => 'meta1',
         'value'   => '1',
         'compare' => '=',
      ), 
 ),
这里也问了同样的问题,但没有得到回答。所以我要打开它。
谢谢

听起来你想让它像一个“或”,其中返回一个与
tax\u查询
meta\u查询
匹配的帖子

不幸的是,这是不可能的使用


您要么需要编写一个,要么只需单独查询它们,然后在PHP端进行检查(如果您知道结果数量相对较少,则可以这样做,但请注意,这不一定会扩展到大型站点).

实现这种查询的唯一方法是使用
$wpdb
全局和SQL语句创建对数据库的自定义查询。我想应该是这样的:

global $wpdb;
$querystr = 
"
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy as term_taxonomy1 ON($wpdb->term_relationships.term_taxonomy_id = term_taxonomy1.term_taxonomy_id)
LEFT JOIN $wpdb->term_taxonomy as term_taxonomy2 ON($wpdb->term_relationships.term_taxonomy_id = term_taxonomy2.term_taxonomy_id)
LEFT JOIN $wpdb->terms as tax1_term1 ON(term_taxonomy1.term_id = tax1_term1.term_id)
LEFT JOIN $wpdb->terms as tax1_term2 ON(term_taxonomy1.term_id = tax1_term2.term_id)
LEFT JOIN $wpdb->terms as tax1_term3 ON(term_taxonomy1.term_id = tax1_term3.term_id)
LEFT JOIN $wpdb->terms as tax2_term1 ON(term_taxonomy2.term_id = tax2_term1.term_id)
LEFT JOIN $wpdb->terms as tax2_term2 ON(term_taxonomy2.term_id = tax2_term2.term_id)
LEFT JOIN $wpdb->terms as tax2_term3 ON(term_taxonomy2.term_id = tax2_term3.term_id)
WHERE 
(
    (
        (term_taxonomy1.taxonomy = 'tax1') 
        AND 
        (tax1_term1.term_id = 1 AND tax1_term2.term_id = 2 AND tax1_term3.term_id = 3)
    )
    OR
    (
        (term_taxonomy2.taxonomy = 'tax2') 
        AND 
        (tax2_term1.term_id = 1 AND tax2_term2.term_id = 2 AND tax2_term3.term_id = 3)
    )
)
OR
(
    $wpdb->postmeta.meta_key = 'meta1' AND $wpdb->postmeta.meta_value = '1'
)
ORDER BY $wpdb->postmeta.meta_value ASC
"
$pageposts = $wpdb->get_results($querystr, OBJECT);

然后,您可以像使用
WP\u查询
循环一样迭代
$pageposts

是否希望它像“或”一样,返回与tax\u查询或meta\u查询匹配的帖子?好吧,使用WP_查询是不可能的,我只是快速浏览了一下,但为了以防万一,您可能需要更详细的查看。你可能需要。你指的另一个问题是什么?谢谢@TimMalone。这就清楚了。我所做的只是单独地询问他们,然后就完成了。没问题@japhfortin,很高兴这对你有帮助!我添加了它作为一个答案,以帮助未来的游客以及。如果您觉得它为您解决了问题,请使用它左侧的复选标记将答案标记为已接受。