1
votes

I have a category archive with a custom taxonomy "client". Now I want to order the category archive by the term of the custom taxonomy (the client name). I tried to query it by meta_value and a advanced custom taxonomy query, but I could not get it to be ordered by the name of the taxonomy term. Any suggestions to get a Wordpress loop ordered by taxonomy term name? See line $query->set( 'orderby', 'how_to_order_by_taxonomy_name' );

Registering the taxonomy

add_action( 'init', 'jp_register_project_taxonomies', 0 );
function jp_register_project_taxonomies() {

    // register taxonomy to hold our clients
    register_taxonomy(
        'client',
        array( 'post' ),
        array(
            'hierarchical' => false,
            'public' => true,
            'query_var' => true,
            'rewrite' => true,
            'labels' => array(
                'name'                => _x( 'Clients', 'taxonomy general name' ),
                'singular_name'       => _x( 'Client', 'taxonomy singular name' ),
                'search_items'        => __( 'Search Clients' ),
                'all_items'           => __( 'All Clients' ),
                'edit_item'           => __( 'Edit Client' ), 
                'update_item'         => __( 'Update Client' ),
                'add_new_item'        => __( 'Add New Client' ),
                'new_item_name'       => __( 'New Client Name' ),
                'menu_name'           => __( 'Client' )
            ),
        )
    );
}

pre_get_posts Query

add_action( 'pre_get_posts', 'jp_project_taxonomy_queries' );

function jp_project_taxonomy_queries( $query ) {

    if(!is_admin() && $query->is_main_query() && is_category() ):

        if (get_query_var('poby') == 'client'):

            $taxonomies = array();
            $tax_order = (get_query_var('po') == 'DESC' ?  'DESC' : 'ASC' );

            foreach (get_terms('client', array('order' => $tax_order)) as $tax ) {
                $taxonomies[] = $tax->name;
            }

            $taxquery = array(
                array(
                    'taxonomy' => 'client',
                    'terms' => $taxonomies,
                    'field' => 'slug',
                )
            );

            $query->set( 'tax_query', $taxquery );
            $query->set( 'orderby', 'how_to_order_by_taxonomy_name' );

        endif;

    endif;

}
1
please be more specific, post the query you used...table info, etcHituptony
Added my code blocks. It works as it should except of the orderby-clause. I don't know how to get the query ordered by taxonomy name. Is there any possibility?Jonas

1 Answers

0
votes

As far as I know, there is no parameter to order a WP_Query by term.

My solution to this problem usually is to create a meta field that contains the term name or the term slug. This field is created when the post is saved using the hook save_post or save_post_{post_type}.

For instance, to order a list of books (post type book) by year of publication (taxonomy date) we can use this function to create the meta field:

add_action( 'save_post_book', 'prefix_save_date_as_meta', 10 );
function prefix_save_date_as_meta ($post_id) { 
    $years = get_the_terms($post_id,'date');

    if( empty($years) )
        return;

    $years_list = wp_list_pluck($years,'name');

    update_post_meta($post_id,'_book_date',$years_list[0]);

    return;
}

And this other function to order the query:

add_filter( 'pre_get_posts', 'prefix_custom_args_for_loops' );
function prefix_custom_args_for_loops( $query ) {
    if ( !is_admin() && is_post_type_archive('book') && $query->is_main_query() ) {
        $query->set( 'orderby','meta_value_num');
        $query->set( 'meta_key','_book_date');
    }
    return $query;
}