I have 2 custom post types Transit and Colors, connected with a relationship - as a transit post has a custom field to select a color post that it will belong, that stores the Color post ID.

I created a sortable admin column in Transits list for Colors, where I echo the Color's post_title.

When sorting, I want to sort alphabetically on Color's post_title, modifying the query it seems it sorts on Color's post ID.

How to set the pre_get_posts query sorted by the meta color's post title instead of its ID that is stored in the meta table?

Is it possible?

Below my pre_get_post function:

add_action( 'pre_get_posts', 'doc_transit_posts_orderby' );
function doc_transit_posts_orderby( $query ) {
  if( ! is_admin() || ! $query->is_main_query() ) {

  if ( 'color' === $query->get( 'orderby') ) {
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'meta_key', 'color' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.