[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 7 posts - 16 through 22 (of 22 total)
  • Author
    Posts
  • #2165819
    David
    Staff
    Customer Support

    So how is the CPT and Categories constructed ?

    For example – is odberatel a Taxonomy or a taxonomy term ?

    #2165861
    Radek

    “odberatel” is category/taxonomy of CPT “reference”. If You want, I can make temp user for admin for Yourself…

    #2165880
    David
    Staff
    Customer Support

    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 πŸ™‚

    #2165893
    Radek

    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.

    #2165910
    David
    Staff
    Customer Support

    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

    #2166085
    Radek

    Thanx, maybe is_tax(); is enough for me if there is not other CPT with custom taxonomy πŸ˜‰

    #2166091
    David
    Staff
    Customer Support

    Aah yes – good call πŸ™‚

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