0

I am probably just overlooking something here, but some pointers would be greatly appreciated.

I am trying to create an event website using Wordpress and the Advanced Custom Fields plugin.

Now the events have two options, they are either One Day Events, or Multiple Day Events.

I have given them fields that the user can put the date in with the date picker.

One Day Event - one_day_event_date Multiple Day Event - event_start_date , event_end_date

(these output YYYYMMDD format: ie: 20140117)

My problem comes when I want to filter the results in the loop.

Ideally, I would like to show the events in date order, using the fields one_day_event_date and event_start_date.

But I want it to filter older events based on the values one_day_event_date and event_end_date

This is the code I have tried so far:

<?php
$currentdate = date("Ymd",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
$args = array(
'post_type' => 'event',
'meta_query' => array(
    array(
        'key' => 'event_end_date',
        'value' => $currentdate,
        'compare' => '>',
        'type' => 'DATE'
    ),
    array(
        'key' => 'one_day_event_date',
        'value' => $currentdate,
        'compare' => '>',
        'type' => 'DATE'
    )
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$query = new WP_Query( $args ); ?>
1
  • rather than 3 meta keys, why not just omit an end date for single day events? then you can add an OR relation to your meta query and just order by start date. also note that to order by a meta value, you have to set a meta_key argument - see WP_Query in Codex.
    – Milo
    Commented Jan 17, 2014 at 17:56

1 Answer 1

1

Is the problem that your filter is working but the sort doesn't work? If so, I believe it's because you need to specify which meta_key you'd like to sort by. For example, you'll need to add either event_end_date or one_day_event_date as the value for meta_key at the top level array.

Try this...

<?php
$currentdate = date("Ymd",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
$args = array(
    'post_type' => 'event',
    'meta_key' => 'event_end_date', // this is where you specify which meta field to sort on
    'meta_query' => array(
        array(
            'key' => 'event_end_date',
            'value' => $currentdate,
            'compare' => '>',
            'type' => 'DATE'
            ),
        array(
            'key' => 'one_day_event_date',
            'value' => $currentdate,
            'compare' => '>',
            'type' => 'DATE'
            )
        ),
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);
$query = new WP_Query( $args );
?>

If you need to sort them both by a start date, for example, I would actually give them both a date called start_date and sort by that... Then the one day events would just not have an end_date (or maybe the end date can just be the same as the start date).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.