Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Events Orderby Meta Value Date

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'
        )
    )
));
like image 542
Jordan Avatar asked Nov 30 '22 11:11

Jordan


2 Answers

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');
?>
like image 118
Jordan Avatar answered Dec 05 '22 20:12

Jordan


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("Ymd"), // date format error
                'compare' => '<='
            )                   
         )
    );
like image 22
WongKongPhooey Avatar answered Dec 05 '22 21:12

WongKongPhooey