1

I have a custom post type that is an Event.

I'm trying to query those Events and only return Events that are in the current month or later. So the event could be in the past, but it has to be in the same month. The event date is a custom field.

So right now I would only want events that were in the month of August or later, and no events prior to August.

Here's my current query, ordered from oldest to newest:

$custom_post_type = 'event';

$args = array(
    'post_type' => $custom_post_type,
    'post_status' => 'publish',
    'meta_key' => 'event_date_and_time',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'posts_per_page' => -1
);

$events = get_posts($args);
1
  • how did you saved time value inside event_date_and_time. Is it unix timestamp? string? Please provide details.
    – Sisir
    Commented Aug 20, 2015 at 5:53

2 Answers 2

1

You can use meta_query, if you date information is saved in custom field. Like this:

$args = array(
    'post_type' => $custom_post_type,
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'event_date_and_time',
            'value' => current_time('Ymd'),
            'compare' => '>='
        )
    ),
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

This code suppose you date is saved in YYYYMMDD format (20150820). And it shows post from today to future, so you will have to change value to "first day of current month".

0

Use meta_query argument in the array.

$custom_post_type = 'event';

$args = array(
    'post_type' => $custom_post_type,
    'post_status' => 'publish',
    'meta_key' => 'event_date_and_time',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'posts_per_page' => -1
    'meta_query' => array(
        array(
            'key' => 'event_date_and_time',
            'value'   => date("Y-m-1"); // starting from 1st of current month. This will change based on how you store dates.
            'compare' => '>=',
            'type' => 'NUMERIC,' // Let WordPress know we're working with numbers
        )
    )
);

$events = get_posts($args);

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.