Products
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