Wordpress Events Orderby Meta Value Date

2019-08-03 15:55发布

I'm having some trouble getting an arguments array to sort an event list by the date in Wordpress. I've found several suggested solutions here on Stack Overflow and elsewhere, but none of the solutions seem to work after lots of trial and error.

It's nothing fancy, and it should be a lot easier than this. Maybe it is easier and I'm just overthinking it? Any help is greatly appreciated.

I've got a custom Date Field inside the Post using the plugin http://www.advancedcustomfields.com, field name in the database is "event_date".

I've tried the following in various forms:

$args = array(
     'post_status'       => 'publish',
     'meta_key'          => 'event_date',
     'orderby'           => 'meta_value_num',
     'order'             => 'DESC',
     'posts_per_page'    => 6,
     'paged'             => $paged,
     'post__not_in'      => $exclude_array
);

$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

$default_excerpt_length = 250;

And:

$args = array(
    'post_status'       => 'publish',
    'meta_key'          => 'event_date',
    'meta_value_num'    => time(),
    'meta_compare'      => '>=',
    'orderby'           => 'meta_value_num',
    'order'             => 'DESC',
    'posts_per_page'    => 6,
    'paged'             => $paged,
    'post__not_in'      => $exclude_array
);

$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

$default_excerpt_length = 250;

And:

$today = date('Y-m-d');

query_posts(array(
    'post_type'         => 'events',
    'posts_per_page'    => 6,
    'paged'             => $paged,
    'meta_key'          => 'event_date',
    'orderby'           => 'meta_value',
    'order'             => 'DESC',
    'meta_query' => array(
        array(
        'key'        => 'event_date',
        'meta-value' => $value,
        'value'      => $today,
        'compare'    => '>=',
        'type'       => 'CHAR'
        )
    )
));

2条回答
再贱就再见
2楼-- · 2019-08-03 16:09

This is eventually what led to the solution I needed: http://www.advancedcustomfields.com/resources/field-types/date-picker/

I ended up changing the Advanced Custom Field setting to yymmdd as recommend via that url.

This is what I used to query the posts:

$args = array(
  'post_status'       => 'publish',
  'posts_per_page'    => 6,
  'paged'             => $paged,
  'meta_key'          => 'event_date',
  'orderby'           => 'meta_value_num',
  'order'             => 'ASC'
);

And this is what I used to adjust the visual output of the date on the page:

<?php
$source = get_field('event_date');
$date = new DateTime($source);
echo $date->format('F j, Y');
?>
查看更多
对你真心纯属浪费
3楼-- · 2019-08-03 16:25

After searching multiple similar posts, I put a solution together from parts of various other SO posts, hopefully it can help somebody else.

I had a custom meta key called "date" (poor naming convention, I know), and this query shows all posts with a custom "date" meta field in the future, sorting by closest to today.

$args = array(
        'post_type' => 'training-course',
        'posts_per_page' => '-1',
        'meta_key' => 'date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'date',
                'value' => date("YYmmdd"),
                'compare' => '<='
            )                   
         )
    );
查看更多
登录 后发表回答