php 产品和帖子在同一页面上的WordPress阵列

myzjeezk  于 2022-11-21  发布在  PHP
关注(0)|答案(1)|浏览(132)

我使用数组在同一页上显示所有产品和帖子:

$args = array( 
 'post_type' => array( 'product', 'post' ),
 'posts_per_page' => 999,
 'orderby'   => 'meta_value_num', 
 'meta_key' => 'score_value',
 'order' => 'DESC'
);
 
$custom_query = new WP_Query($args);

现在我想过滤帖子'cat' => '82,85'和产品'product_cat' => 96
我已经将代码片段添加到我的代码中,但之后没有显示任何帖子或产品。

$args = array( 
 'post_type' => array( 'product', 'post' ),
 'cat' => '82,85',
 'product_cat' => 96,
 'posts_per_page' => 999,
 'orderby'   => 'meta_value_num', 
 'meta_key' => 'score_value',
 'order' => 'DESC'
);
 
$custom_query = new WP_Query($args);

我应该如何修改我的代码,这两个职位类型和所有职位和产品的类别将显示?
谢谢你的好意,
马格斯

c2e8gylq

c2e8gylq1#

您可以使用tax_query来执行此操作。

$args = array(
    'post_type' => array('product', 'post'),
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => ['82', '85'],
        ),
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'    => ['96'],
        ),
    ),
    'posts_per_page' => 999,
    'orderby'        => 'meta_value_num',
    'meta_key'       => 'score_value',
    'order'          => 'DESC',
);

$custom_query = new WP_Query($args);

相关问题