wordpress custom_meta时间戳的排序

时间:2014-04-16 17:55:39

标签: mysql wordpress sorting

所以我有一个wp元查询处理搜索具有开始时间戳和结束时间戳的事件,我想返回用户在搜索中选择的两个日期内的所有结果。到目前为止,这部分工作正常。

但是,我想按开始时间戳对结果进行排序,因此最近的事件会先出现。

$args = array(
    's' => $_GET['k'],
    'post_type' => 'event',
    'posts_per_page' => '-1',
    'post_status' => array( 'private','publish' ),

    'meta_key' =>'_start_ts',
    'orderby' => 'meta_value_num',
    'order' => 'asc',

    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => '_end_ts',
            'value' => array(strtotime($_GET['d1']),strtotime($_GET['d2'])),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        ),
        array(
            'key' => '_start_ts',
            'value' => array(strtotime($_GET['d1']),strtotime($_GET['d2'])),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        )
    )
);

如果我在4月份查看所有活动,我们会收到以下时间戳:

1397865600, 1398211200, 1398384000, 1397520000, 1398297600

1 个答案:

答案 0 :(得分:0)

我的数据库中有相同类型的事件。使用你的args并且查询执行得很漂亮。一个区别是我的日期来自数据库yyyymmdd值。我正在使用以这种格式保存的高级自定义字段插件。

这是我改编的代码

$args = array(
    's' => 'old',
    'post_type' => 'event',
    'posts_per_page' => '-1',
    'post_status' => array( 'private','publish' ),

    'meta_key' =>'start_date',
    'orderby' => 'meta_value_num',
    'order' => 'asc',

    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'end_date',
            'value' => array(strtotime('20130101'),strtotime('20140101')),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        ),
        array(
            'key' => 'start_date',
            'value' => array(strtotime('20130101'),strtotime('20140101')),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        )
    )
);
$search = new WP_Query($args);

if ( $search->have_posts() ):

    setlocale(LC_ALL, 'nl_NL.UTF-8');

    while ( $search->have_posts() ) : $search->the_post();

        $start_date = get_post_meta(get_the_ID(), 'start_date', true);
        $end_date = get_post_meta(get_the_ID(), 'end_date', true);
        $start = strftime( '%A %e %B %Y', strtotime( $start_date ) );
        $end = strftime( '%A %e %B %Y', strtotime( $end_date ) );


        echo "$start ($start_date) - $end ($end_date) <br />";

    endwhile;
endif;

结果

zaterdag 3 mei 2014 (20140503) - zaterdag 3 mei 2014 (20140503) 
zondag 4 mei 2014 (20140504) - zondag 4 mei 2014 (20140504) 
zondag 4 mei 2014 (20140504) - zondag 4 mei 2014 (20140504) 
zondag 11 mei 2014 (20140511) - zondag 11 mei 2014 (20140511) 
vrijdag 16 mei 2014 (20140516) - zondag 18 mei 2014 (20140518) 
zaterdag 17 mei 2014 (20140517) - zaterdag 17 mei 2014 (20140517) 
zondag 18 mei 2014 (20140518) - zondag 18 mei 2014 (20140518) 
zondag 1 juni 2014 (20140601) - zondag 1 juni 2014 (20140601) 
zondag 1 juni 2014 (20140601) - zondag 1 juni 2014 (20140601) 
zondag 15 juni 2014 (20140615) - zondag 15 juni 2014 (20140615) 
donderdag 26 juni 2014 (20140626) - donderdag 26 juni 2014 (20140626) 
zondag 29 juni 2014 (20140629) - zondag 29 juni 2014 (20140629) 
zaterdag 5 juli 2014 (20140705) - zondag 6 juli 2014 (20140706) 
zondag 6 juli 2014 (20140706) - zondag 6 juli 2014 (20140706) 
zondag 6 juli 2014 (20140706) - zondag 6 juli 2014 (20140706) 
zondag 10 augustus 2014 (20140810) - zondag 10 augustus 2014 (20140810) 
zondag 17 augustus 2014 (20140817) - zondag 17 augustus 2014 (20140817) 
zondag 7 september 2014 (20140907) - zondag 7 september 2014 (20140907) 
zaterdag 13 september 2014 (20140913) - zaterdag 13 september 2014 (20140913) 
zondag 14 september 2014 (20140914) - zondag 14 september 2014 (20140914) 
zondag 28 september 2014 (20140928) - zondag 28 september 2014 (20140928) 
zondag 5 oktober 2014 (20141005) - zondag 5 oktober 2014 (20141005)