Archived topic
CPT archive page, all posts order by CPT's category/taxonomy
21 replies · Started by Radek on March 22, 2022
Can you please provide me a simple snippet to order posts on CPT (called "reference") archive page by its CPT category (called "rok"). For CPT I use PODs and for archive page (link at private info) I use Content Template Block Element by GP with Location rule to CPT's archive. CPT's category ("rok") is without any custom fields, just category itself and contains values: 2016, 2017, 2018 ...
I found this at PODs doc: https://docs.pods.io/code-snippets/ordering-cpt-archive-by-custom-fields/ but I'm bad with PHP, so I can't work it out and I mean it's also old code if there is no $WP_query, just $query.
Thanx a lot for Your help.
Hi there,
sorting posts by tax terms isn't something that WP can do natively.Several responses I found reference this reply:
https://wordpress.stackexchange.com/a/14309
I think it may be possible if you could save the tax term or at least its orderby value as a Custom Field.
OK, let's say I'll add field "year" to CPT "reference", is then archive page be ordered by this field ?
Yeah then you should be able to do something like this:
function custom_cpt_order($query){
if( !is_admin() && is_post_type_archive('your_custom_post_type') && 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' );
Hi, If I use Your snippet:
function custom_reference_order($query){
if( !is_admin() && is_post_type_archive('reference') ){
$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' );
It breaks Content Template Block Element view (there is only standard list), look at link from private info
EDIT: it breaks complete page view. Only logo and list of CPT's name, no menu, no header, no footer...
Hmm...
try including && empty( $query->query_vars['suppress_filters'] in the condition. I edited the code above to show where:
so now my snippet looks like this:
function custom_reference_order($query){
if( !is_admin() && is_post_type_archive('reference') && 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' );
Only thing the menu has been added...
Archive pages of CPT "reference" filtered by any subcategory is not affected and looks the way it should (private link)
Can you try adding this extra snippet ( keep the existing one you have too ):
add_filter( 'generate_elements_custom_args', function( $args ) {
$args['suppress_filters'] = true;
return $args;
} );
It works, CPT archive page looks the way it should and posts are ordered by custom field. Great. Is it final solution ?
Oh, if I click on taxonomy link (filter archive page by one of categories), sub archive pages are not ordered. Can we add some condition to this:
if( !is_admin() && is_post_type_archive('reference') && empty( $query->query_vars['suppress_filters'] ) )
to work with subarchive pages too ?
Thanx
Well we're getting there :)
Instead of:
if( !is_admin() && is_post_type_archive('reference') && empty( $query->query_vars['suppress_filters'] ){
Try:
if( !is_admin() && 'reference' == get_post_type() && !is_single() && empty( $query->query_vars['suppress_filters'] ){
You mean add another function snippet or edit existing ? I tried edit existing and it does not work and it breaks existing ordering of parent (main) CPT archive page. Snippet looks likethis now:
function custom_reference_order($query){
if( !is_admin() && 'reference' == get_post_type() && !is_single() && 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' );
May be I had in mind some condition like this: if ( is_post_type_archive( 'reference' ) || is_tax() ) {
condition that works at generate_blog_columns filter...
Edit your existing snippet.
So you total code would be something like this:
add_filter( 'generate_elements_custom_args', function( $args ) {
$args['suppress_filters'] = true;
return $args;
} );
function custom_cpt_order($query){
if( !is_admin() && 'reference' == get_post_type() && !is_single() && 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_cpt_order' );
Yeah, my code:
function custom_reference_order($query){
if( !is_admin() && 'reference' == get_post_type() && !is_single() && 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' );
And like a wrote, it breaks ordering on CPT archive page and ordering on subarchive pages doesn't work either.
links at private
output view of ordering meta field lokks like: Rok: xxxx where xxxx is from 2021 to 2016 and on last archive page (9) there are 2018 and 2019, so working ordering from last snippet before update is gone...