How to order by multiple meta keys?

2019-02-03 04:59发布

问题:

I’m using a custom loop to display my events on a page, I get it fine ordering by the event start date using the below:

$args = array( 
    'post_type' => 'event',
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_key' => '_event_start_date');
$loop = new WP_Query( $args );

But the meta_key option only allows one value. How to use two values (_event_start_date and _event_start_time)?

回答1:

This is something that WordPress added support for in 4.2: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

In your case you'll probably want to do something like this:

$args = array(
    'post_type'  => 'event',
    'meta_query' => array(
        'relation' => 'AND',
        'event_start_date_clause' => array(
            'key'     => '_event_start_date',
            'compare' => 'EXISTS',
        ),
        'event_start_time_clause' => array(
            'key'     => '_event_start_time',
            'compare' => 'EXISTS',
        ), 
    ),
    'orderby' => array(
        'event_start_date_clause' => 'ASC',
        'event_start_time_clause' => 'ASC',
    ),
);

$loop = new WP_Query( $args );


回答2:

You have to use meta_query at add your meta keys as everyone said, but also you have to customize order by function.

$args = array(
    'post_type' => 'event',
    'meta_key' => '_event_start_date',
    'meta_query' => array(
        array('key' => '_event_start_date'),
        array('key' => '_event_start_time')
    ),
);

add_filter('posts_orderby','customorderby');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','customorderby');

And put this on your functions.php

function customorderby($orderby) {
    return 'mt1.meta_value ASC, mt2.meta_value ASC';
}

You can read more at http://dotnordic.se/sorting-wordpress-posts-by-several-numeric-custom-fields/



回答3:

From WP_Query documentation, use meta_query and fill up with an array of meta key/values:

$args = array(
    'post_type' => 'product',
    'meta_query' => array(
        array(
            'key' => 'color',
            'value' => 'blue',
            'compare' => 'NOT LIKE'
        ),
        array(
            'key' => 'price',
            'value' => array( 20, 100 ),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
    )
);
$query = new WP_Query( $args );


回答4:

You can order on one key, of multiple. Like so:

        $events = get_posts(array(
           'post_type' => 'my_event',
           'post_status' => 'publish',
           'meta_query' => array(
               array(
                   'key'    =>  'my_event_type',
                   'value'  =>  'aGoodOne'
               ),

               array(
                   'key'    =>  'my_event_start',
                   'value'  =>  time(), // in this case, only grabbing upcoming events
                   'compare'=>  '>',
                   'type'   =>  'NUMERIC' //  assuming value is unix timestamp
               )
           ),
           'meta_key'   =>  'my_event_start', // we're sorting on the start time key
           'orderby'    =>  'meta_value_num', // it's a numeric value
           'order'      =>  'ASC' // in chrono order
        ));

The key here is that meta_key specifies which key we're ordering by, of the ones we use to grab our records with meta_query.



回答5:

  add_action( 'pre_get_posts', 'pre_get_posts_programpunkter' );

function pre_get_posts_programpunkter( $query ) {
	
	if( !is_admin()  && is_post_type_archive( 'programpunkt' ) && $query->is_main_query() ) {
		
		$query->set( 'orderby', 'meta_value' );
		$query->set( 'meta_key', 'event-date' );
			
		$query->set( 'meta_query', array(
			'relation' => 'AND', 
				array( 'key' => 'event-date', 'compare' => 'EXISTS' ), 
				array( 'key' => 'event-start', 'compare' => 'EXISTS' ) 
			) 
		);
			
		$query->set( 'order', 'ASC' );
				
	}
}

add_filter('posts_orderby', 'programpunkter_orderby');

function programpunkter_orderby( $orderby ) {
	
	if( get_queried_object()->query_var === 'programpunkt' )  {
		
		global $wpdb;		
		$orderby = str_replace( $wpdb->prefix.'postmeta.meta_value', 'mt1.meta_value, mt2.meta_value', $orderby );	
		
	}	
	return $orderby;
}