Title: Orderby two custom fields in same column
Last modified: July 8, 2019

---

# Orderby two custom fields in same column

 *  [Viktor123b](https://wordpress.org/support/users/viktorbengtsson8/)
 * (@viktorbengtsson8)
 * [7 years ago](https://wordpress.org/support/topic/orderby-two-custom-fields-in-same-column/)
 * Hey,
 * I have a list of posts, one of the columns is populated with one of two different
   custom fields, one is a regular custom field and the other is a post object field
   pointing to another custom post type.
    I want that column to be in alphabetical
   order. The query I have come up with so far is: (but stil not in correct order..)
   customer_name is the post object of which the title of post that is displayed.
   quick_customer_name is a regular custom field.
 *     ```
       'meta_query' => array(
       	'relation' => 'OR',
       	'customer_name' => array(
       		'key' => 'customer_name',
       		'compare' => 'EXISTS',
       	),
       	'quick_customer_name' => array(
       		'key' => 'quick_customer_name',
       		'compare' => 'EXISTS',
       	),
       ),
       'orderby' => array(
       	'customer_name' => 'desc',
       	'quick_customer_name' => 'desc',
       )
       );
       ```
   
 * Anyone have an idea of what I am doing wrong?
 * All help is very appriciated!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Anil Ankola](https://wordpress.org/support/users/anilankola/)
 * (@anilankola)
 * [7 years ago](https://wordpress.org/support/topic/orderby-two-custom-fields-in-same-column/#post-11713099)
 * Hi, [@viktorbengtsson8](https://wordpress.org/support/users/viktorbengtsson8/)
   
   use this Query
 *     ```
       $query_options = array(	
       	'post_type'     => 'POST_TYPE_NAME_HERE',
       	'posts_per_page' => -1,
       	'meta_query' => array(
       		'customer_name' => array(
       			'key' => 'customer_name',
       			'compare' => 'LIKE',
       		),
       		'quick_customer_name' => array(
       			'key' => 'quick_customer_name',
       			'compare' => 'LIKE',
       		),
   
       	),
       	'orderby' => array(
       		'customer_name' => 'desc',
       		'quick_customer_name' => 'desc',
       	)
       );
       ```
   
 *  Thread Starter [Viktor123b](https://wordpress.org/support/users/viktorbengtsson8/)
 * (@viktorbengtsson8)
 * [7 years ago](https://wordpress.org/support/topic/orderby-two-custom-fields-in-same-column/#post-11713154)
 * Hi [@anilankola](https://wordpress.org/support/users/anilankola/), thanks for
   the reply!
 * When i changed EXISTS to LIKE I got a similar result as before where rows that
   had one of those fields came on top, just not in alphabetical order. When I removed
   the relation OR only rows with ‘customer_name’ appeared and not those with ‘quick_customer_name’
   or with empty values.
 * Maybe I should have posted the entire query. (left the relation OR where it is
   below)
 *     ```
       $posts_per_page = 10;
       $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
       $args = array(
           'post_type' => 'offerter',
           'post_status' => 'publish',
           'posts_per_page' => $posts_per_page,
           'paged' => $paged,
           'meta_key' => 'business_id',
           'meta_value' => $business_id,
           'meta_query' => array(
               'relation' => 'OR',
               'customer_name' => array(
                   'key' => 'customer_name',
                   'compare' => 'LIKE',
               ),
               'quick_customer_name' => array(
                   'key' => 'quick_customer_name',
                   'compare' => 'LIKE',
               ),
           ),
           'orderby' => array(
               'customer_name' => 'desc',
               'quick_customer_name' => 'desc',
           )
       ```
   
    -  This reply was modified 7 years ago by [Viktor123b](https://wordpress.org/support/users/viktorbengtsson8/).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Orderby two custom fields in same column’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [Viktor123b](https://wordpress.org/support/users/viktorbengtsson8/)
 * Last activity: [7 years ago](https://wordpress.org/support/topic/orderby-two-custom-fields-in-same-column/#post-11713154)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
