[Support request] CPT archive page, all posts order by CPT’s category/taxonomy

Home Forums Support [Support request] CPT archive page, all posts order by CPT’s category/taxonomy

Home Forums Support CPT archive page, all posts order by CPT’s category/taxonomy

  • This topic has 21 replies, 2 voices, and was last updated 2 years ago by David.
Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #2163130
    Radek

    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.

    #2163333
    David
    Staff
    Customer Support

    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.

    #2163667
    Radek

    OK, let’s say I’ll add field “year” to CPT “reference”, is then archive page be ordered by this field ?

    #2163678
    David
    Staff
    Customer Support

    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' );
    #2164402
    Radek

    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…

    #2164587
    David
    Staff
    Customer Support

    Hmm…

    try including && empty( $query->query_vars['suppress_filters'] in the condition. I edited the code above to show where:

    https://generatepress.com/forums/topic/cpt-archive-page-all-posts-order-by-cpts-category-taxonomy/#post-2163678

    #2164915
    Radek

    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…

    #2165503
    Radek

    Archive pages of CPT “reference” filtered by any subcategory is not affected and looks the way it should (private link)

    #2165651
    David
    Staff
    Customer Support

    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;
    } );
    #2165662
    Radek

    It works, CPT archive page looks the way it should and posts are ordered by custom field. Great. Is it final solution ?

    #2165674
    Radek

    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

    #2165713
    David
    Staff
    Customer Support

    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'] ){

    #2165733
    Radek

    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…

    #2165739
    David
    Staff
    Customer Support

    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' );
    #2165755
    Radek

    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…

Viewing 15 posts - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.