2

I am attempting to orderby the author_name when you click on the Author arrow on a list of tickets. When you click the arrow, the names are ordering by something I cannot identify. I want them to order by the actual name and not the ID number for the user.

How do I get the orderby to query by the "nice name" of the user(author)?

public function add_support_ticket_author_query( $query )
{
    if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
        switch( $orderby ) {
            case 'author':
                $query->set( 'orderby', 'author' );
                break;
        }
    }
}

1 Answer 1

3

To order by the authors nicename is not nativly supported by the WP_Query. If you order by author, as you have experienced, you order by the ID of the author.

So, you would need to actually go into the SQL clauses and manage this by yourself.

This is a quick and dirty hack to do so:

<?php

add_action( 'pre_get_posts', function( $query ) {

    if (
        ! $query->is_main_query() 
        || 'author' !== $query->get( 'orderby' )
    ) {
        return;
    }

    add_filter( 'posts_clauses', '177581_add_nicename_orderby', 10, 2 );
} );

function 177581_add_nicename_orderby( $clauses, $query ) {

    global $wpdb;

    $clauses['join'] = 'LEFT JOIN ' . $wpdb->users . ' AS author ON ( ' . $wpdb->posts . '.post_author = author.ID)';
    $clauses['orderby'] = 'author.user_nicename ' . $query->get( 'order' );
    return $clauses;
}

What we need to do: In the first step, we check, if we want to order by author, what we treat equally to order by its nicename. If this is the case, we hook into the posts_clauses-filter, where we can actually alter the SQL statements.

We have to

  1. Join the user table, as we find the nicename in this table.
  2. Alter our orderby statement accordingly.
1
  • Haven't tested but looks good to me. @ILikeTurtles please consider accepting this answer if it worked to keep the WPSE site format tidy. Commented Apr 5, 2018 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.