20

我正在使用自定义循环在页面上显示我的事件,我可以使用下面的事件开始日期很好地排序:

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

但是 meta_key 选项只允许一个值。如何使用两个值(_event_start_date_event_start_time)?

4

5 回答 5

20

这是 WordPress 在 4.2 中添加的支持:https ://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

在你的情况下,你可能想要做这样的事情:

$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 );
于 2016-02-20T00:50:31.363 回答
1

正如每个人所说,您必须在添加元键时使用 meta_query,但您还必须按功能自定义顺序。

$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');

把它放在你的functions.php上

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

您可以在http://dotnordic.se/sorting-wordpress-posts-by-several-numeric-custom-fields/阅读更多内容

于 2015-11-23T13:04:16.237 回答
-1

  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;
}

于 2015-02-13T19:36:45.660 回答
-1

WP_Query 文档中,使用meta_query并填写一组元键/值:

$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 );
于 2013-07-19T12:24:27.463 回答
-1

您可以按一个键或多个键订购。像这样:

        $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
        ));

这里的关键是meta_key指定我们使用哪个键来排序,我们用来获取我们的记录meta_query

于 2013-11-12T23:18:24.340 回答