网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

Wordpress - CPT和分类法页面上的自定义顺序

GG网络技术分享 2025-03-18 16:12 3


问题描述:

I am creating a function to order a specific custom post type (ex : produit). It works well on the CPT archive page but it doesn\'t work on the custom taxonomy pages (ex: categories) :

function my_pre_get_posts( $query ) {

// do not modify queries in the admin

if( is_admin() ) {

return $query;

}

// only modify queries for \'produit\' post type

if( isset($query->query_vars[\'post_type\']) && $query->query_vars[\'post_type\'] == \'produit\') {

$query->set(\'orderby\', \'meta_value\');

$query->set(\'meta_key\', \'irank\');

$query->set(\'order\', \'DESC\');

}

// return

return $query;

}

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

How can I append my code so the custom order applies also to my custom taxonomy pages ?

Thanks a lot

网友观点:

You\'ll want to use the is_tax() function to check if it\'s the taxonomy archive you want to alter the sort order on. So something like this...

if ( $query->is_tax( \'produit-category\' ) && $query->is_main_query() ) {

$query->set(\'orderby\', \'meta_value\');

$query->set(\'meta_key\', \'irank\');

$query->set(\'order\', \'DESC\');

}

标签:

提交需求或反馈

Demand feedback