Support » Developing with WordPress » Orderby two custom fields in same column

  • Viktor123b

    (@viktorbengtsson8)


    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)
  • Hi, @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',
    	)
    );
    
    
    Viktor123b

    (@viktorbengtsson8)

    Hi @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 4 months, 1 week ago by Viktor123b.
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.