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


WP_Query - Display Custom Posts In Specific Category

我正在尝试运行一个查询,从单个类别返回我的自定义帖子。我尝试过按类别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 );

如果您使用的是自定义分类法,那么您需要修改您的查询,如下所示:

$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 );

希望它能帮助你。

他错过了一个"quot;这里的好代码:

$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 );