获取帖子类型和类别

问题描述 投票:0回答:1

在我的首页(index.php)上,我想显示“事件”类型的所有帖子以及“帖子”类型的类别“主要”。我怎样才能合并这两个条件?在我当前的代码中,我可以过滤两种帖子类型,但不能过滤类别“main”。

<?php global $wp_query; $args = array_merge( $wp_query->query, array( 'post_type' => array('post','event') ));
query_posts( $args ); ?>
php wordpress post categories
1个回答
1
投票

只需将 $args 更改为:

<?php
$args = array(
    'post_type' => array('post','event'),

    'tax_query' => array(
          'relation' => 'OR',
       array(
         'taxonomy' => 'category',
         'terms' => 'main',
         'field' => 'slug'
       ),
       array(
         'taxonomy' => 'event_tag', // this needs to be whatever custom taxonomy you have declared for your custom post type.
         'terms' => 'main',
         'field' => 'slug'
       ),
    )
);
?>
© www.soinside.com 2019 - 2024. All rights reserved.