Archived topic
CPT archive page, all posts order by CPT's category/taxonomy
21 replies · Started by Radek on March 22, 2022
So how is the CPT and Categories constructed ?
For example - is odberatel a Taxonomy or a taxonomy term ?
"odberatel" is category/taxonomy of CPT "reference". If You want, I can make temp user for admin for Yourself...
Ok so what about your idea where check the is_tax:
function custom_cpt_order($query){
if( !is_admin() && ( is_post_type_archive('reference') || is_tax('odberatel') ) && empty( $query->query_vars['suppress_filters'] ){
$query->query_vars['meta_key'] = 'your_meta_key';
$query->query_vars['orderby'] = 'meta_value';
$query->query_vars['order'] = 'DESC';
}
return $query;
}
add_filter( 'pre_get_posts', 'custom_cpt_order' );
Please double check my spelling :)
Works perfect, I added another taxonomy "prumysl" to condition too:
function custom_reference_order($query){
if( !is_admin() && ( is_post_type_archive('reference') || is_tax('odberatel') || is_tax('prumysl') ) && empty( $query->query_vars['suppress_filters'] ) ){
$query->query_vars['meta_key'] = 'ref-rok';
$query->query_vars['orderby'] = 'meta_value';
$query->query_vars['order'] = 'DESC';
}
return $query;
}
add_filter( 'pre_get_posts', 'custom_reference_order' );
Thanx a lot David for Your great work and help as always.
Awesome - really glad to hear that.
Just for reference the is_tax() supports arrays:
https://developer.wordpress.org/reference/functions/is_tax/#comment-4984
Glad to be of help
Thanx, maybe is_tax(); is enough for me if there is not other CPT with custom taxonomy ;)
Aah yes - good call :)