按分类排序自定义帖子类型

时间:2021-09-17 作者:raphel

对于所有我用这段代码按ASC方式按特定类别对列表自定义帖子类型进行排序的人:(我有“model”自定义帖子类型,我想按“model year”分类法显示顺序

 function my_pre_get_posts( $query ) {

 // do not modify queries in the admin
 if( is_admin() ) {

return $query;

}

// only modify queries for \'model\' post type
if ( $query->is_tax( \'model-year\' ) && $query->is_main_query() ) {

$query->set(\'orderby\', \'meta_value\');   
$query->set(\'meta_key\', \'\');    
$query->set(\'order\', \'ASC\'); 
}

// return
return $query;

 }

 add_action(\'pre_get_posts\', \'my_pre_get_posts\');

1 个回复
SO网友:Dharmesh

添加元键值:

function my_pre_get_posts( $query ) {

    // do not modify queries in the admin
    if( is_admin() ) {
        return $query;
    }

    // only modify queries for \'model\' post type
    if ( $query->is_tax( \'model-year\' ) && $query->is_main_query() ) {
        $query->set(\'orderby\', \'meta_value\'); 

        // Add Metakey ( "Custome field name" );  
        $query->set(\'meta_key\', \'order_number\');    
        $query->set(\'order\', \'ASC\'); 
    }
    return $query;
}
add_action(\'pre_get_posts\', \'my_pre_get_posts\');

相关推荐