0

I have a custom post type called 'events', I have a page which displays events by each month. Each event has a start_date and finish_date custom field (using advanced custom fields plugin) which stores the date as yymmdd. If an event spans over two different months it is only appearing in the month of the start date. I'm stuck on how to show it in the month of the finish date as well. For example, if I have an event with a start date of 19/03/2014 and a finish date of 19/04/2013 it should appear in both the month of March and April.

I am using the below:

// Get chosen month to display from URL
$events_month = sanitize_text_field($_GET["month"]);
$events_year = sanitize_text_field($_GET["year"]);

// Convert chosen month to display to a timestamp
$ts = strtotime("$events_month $events_year");

// Create chosen month start end end dates to use for query
$month_start_date = date('Ym01', $ts);
$month_end_date = date('Ymt', $ts);

$args = array(
    'post_type' => 'events',
    'posts_per_page' => 50,
    'order' => 'ASC',
    'orderby' => 'meta_value_num',
    'meta_key' => 'start_date',
    'meta_query'        => array(
        array(
            'key' => 'start_date',
            'value' => array($month_start_date, $month_end_date),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
    )
);

$events = new WP_Query($args);
1

The query will by default GROUP BY the post ID, meaning that a post will only appear once in the list. You can remove that GROUP BY pretty easily with a filter:

add_filter('posts_groupby','__return_empty_string');
$events = new WP_Query($args);
remove_filter('posts_groupby','__return_empty_string');

That might get you what you want. It is a bit hard to tell. Without the GROUP BY your post should show up multiple times in the list. I suspect that you will still have to loop over the results and organize them to get exactly what you want though.

See:
https://codex.wordpress.org/Function_Reference/_return_empty_string

  • Hi thanks for the suggestion but I don’t think you’ve understood what I’m trying to do. Going back to my example: If an event spans over two different months it is only appearing in the month of the start date. For example, if I have an event with a start date of 19/03/2014 and a finish date of 19/04/2013 it should appear in both the month of March and April, but not for now it can only appear in March, because in April it will fail when checking whether the start date is greater that the month start date (it started the month before) Any other ideas? – user1280853 Mar 19 '14 at 15:51
  • If you have dates in this format-- 19/03/2014-- you have a big problem. Those aren't going to sort right. I'll think about the problem more. – s_ha_dum Mar 19 '14 at 16:08
  • That's just the format to output them, I can store them in the database as timestamps or dates – user1280853 Mar 19 '14 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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