- This topic has 21 replies, 2 voices, and was last updated 2 years ago by David.
-
AuthorPosts
-
March 22, 2022 at 2:39 am #2163130Radek
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.March 22, 2022 at 5:48 am #2163333DavidStaffCustomer SupportHi 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.
March 22, 2022 at 8:54 am #2163667RadekOK, let’s say I’ll add field “year” to CPT “reference”, is then archive page be ordered by this field ?
March 22, 2022 at 9:06 am #2163678DavidStaffCustomer SupportYeah 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' );
March 23, 2022 at 2:12 am #2164402RadekHi, 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…
March 23, 2022 at 5:44 am #2164587DavidStaffCustomer SupportHmm…
try including
&& empty( $query->query_vars['suppress_filters']
in the condition. I edited the code above to show where:March 23, 2022 at 8:21 am #2164915Radekso 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…
March 24, 2022 at 12:45 am #2165503RadekArchive pages of CPT “reference” filtered by any subcategory is not affected and looks the way it should (private link)
March 24, 2022 at 3:32 am #2165651DavidStaffCustomer SupportCan 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; } );
March 24, 2022 at 3:40 am #2165662RadekIt works, CPT archive page looks the way it should and posts are ordered by custom field. Great. Is it final solution ?
March 24, 2022 at 3:55 am #2165674RadekOh, 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 ?
ThanxMarch 24, 2022 at 4:35 am #2165713DavidStaffCustomer SupportWell 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'] ){
March 24, 2022 at 4:55 am #2165733RadekYou 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…March 24, 2022 at 5:00 am #2165739DavidStaffCustomer SupportEdit 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' );
March 24, 2022 at 5:24 am #2165755RadekYeah, 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… -
AuthorPosts
- You must be logged in to reply to this topic.