WP_Query - 显示特定类别中的自定义帖子

时间:2015-04-07 04:00:58

标签: php wordpress wp-query

我尝试运行一个从单个类别返回自定义帖子的查询。我已尝试过类别ID('cat'=> 83,)和类别slug('category_name => 'slugname'),但两者都返回空结果。

$custom_args = array(

'post_type' => 'event',
'cat'    => 83,
'posts_per_page' => 10,
'paged' => $paged,
'meta_key' => 'event_date', // name of custom field
'orderby' => 'meta_value_num',
'order' => 'ASC'
);

$custom_query = new WP_Query( $custom_args );

1 个答案:

答案 0 :(得分:0)

如果您使用自定义分类,则需要修改查询,如下所示:

$custom_args = array(

'post_type' => 'event',
'tax_query' => array(
        array(
            'taxonomy' => 'custom taxonomy',
            'field' => 'slug', //can be set to ID
            'terms' => 'bob' //if field is ID you can reference by cat/term number
        )
    )
'posts_per_page' => 10,
'paged' => $paged,
'meta_key' => 'event_date', // name of custom field
'orderby' => 'meta_value_num',
'order' => 'ASC'
);

$custom_query = new WP_Query( $custom_args );

希望它对你有所帮助。