首页 > 解决方案 > WP_Query - 按分类术语的元数据排序

问题描述

我正在尝试使用特定分类法的术语值为 wp_query 订购帖子

每个分类术语都有一个由元数据设置的从 1 到 10 的数字(使用 ACF)

$args = array(

    'post_type'         => 'post',
    'post_status'       => 'publish',
    'tax_query'         => array(
                      'relation' => 'AND',
                             array(
                                  'taxonomy'          => 'category',
                                  'meta_key'          => 'menu_order',
                                  'orderby'           => 'meta_value_num',
                             ),
                       ),
);
$wp_query = new \WP_Query($args);

基本上每个术语都有一个顺序,我希望查询返回所有帖子,但我手动设置了这个顺序!

我尝试过的另一个解决方案是使用https://wordpress.org/plugins/custom-taxonomy-order-ne/'orderby' => 'term_order'它给出了

$terms = get_terms( 'category',array(
    'orderby'   => 'term_order'
));

$term_ids = wp_list_pluck( $terms, 'term_id' );

$tax_query = array(
    'relation'  => 'OR',
            array(
                'taxonomy'          =>  'category',
                'field'             => 'term_id',
                'terms'              => $term_ids,
            ),
    );
}

$wp_query = new \WP_Query($args);

标签: phpwordpress

解决方案


推荐阅读