• Resolved florushj

    (@florushj)


    Thanks for this lovely plug-in!

    I’m having trouble figuring out how to effectively use the plug-in in a custom WP Query (inside a meta query).

    My set up is as follows:

    – I created a custom post type: event.
    – I created a (conditional) custom start date field, an ‘is_recurring’ true/false field and a (conditional) recurring field.
    – I created some event posts. Some are recurring (using the plug-in) and some have a single fixed starting date using the standard ACF date field.

    I would like to create a WP Query that retrieves all event posts where the start date is in the future. Or better in the upcoming week/month.

    I have no issue creating this for just the events that have a fixed starting date, but I’m stuck trying to make the query also take into account the start date of the recurring events.

    Thanks for your help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Marc Bellêtre

    (@marcbelletre)

    Hi @florushj,

    Thank you for the kind feedback ??

    The plugin automatically sets a start_date and a end_date post meta when saving the field. So you can basically get all the future events by adding the following meta_queries: you want to retrieve all events that started before the end date (1 month after the start date), and that will end after the start date.

    $start = new DateTime();
    
    // Set the end date to the next month (or use 'P1W' for the next week)
    $end = clone $start;
    $end->add(new DateInterval('P1M'));
    
    // Set both dates to midnight
    $start->setTime(0,0,0);
    $end->setTime(0,0,0);
    
    new WP_Query([
        'post_type' => 'event',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'meta_query' => [
            'relation' => 'AND',
            [
                'key' => 'start_date',
                'compare' => '<=',
                'value' => $end->format('Y-m-d'),
                'type' => 'DATE',
            ], [
                'key' => 'end_date',
                'compare' => '>=',
                'value' => $start->format('Y-m-d'),
                'type' => 'DATE',
            ],
        ],
    ]);

    Hope this helps ??

    Cheers,
    Marc

    Thread Starter florushj

    (@florushj)

    Thanks for your quick response. However, I’m not able to get the code provided to work. My guess is that it has something to do with the following:

    You say that start_date and end_date post meta is set when saving the post, but when I check the database after saving a post with a recurring rule I can see the recurring rule is saved as post meta (in my case with meta_key recurring_rule). Like so:

    FREQ=DAILY;UNTIL=20220430T000000;DTSTART=20220401T000000;INTERVAL=1

    But there are no separate start_date and end_date meta_key named post meta fields? A meta query for the key start_date and end_date therefore can not work?

    Also, my second guess would have been the ->format('Y-m-d') part which I think should be ->format('Ymd')?

    Edit: After browsing through the plugin code I saw that the default return format was set to Y-m-d.

    • This reply was modified 2 years, 7 months ago by florushj.
    • This reply was modified 2 years, 7 months ago by florushj.
    • This reply was modified 2 years, 7 months ago by florushj.
    • This reply was modified 2 years, 7 months ago by florushj.
    • This reply was modified 2 years, 7 months ago by florushj.

    and how can I get only the first one upcoming event after today?

    Thread Starter florushj

    (@florushj)

    @marcbelletre Could you possibly tell me if the code you provided works in your case?

    @thenax If the code provided by @marcbelletre works your case would be solved by setting posts per page to 1 and removing the end_date part of the meta_query. Like so. BUT (!) I did not get this to work.

    $start = new DateTime();
    
    // Set the end date to the next month (or use 'P1W' for the next week)
    $end = clone $start;
    $end->add(new DateInterval('P1M'));
    
    // Set both dates to midnight
    $start->setTime(0,0,0);
    $end->setTime(0,0,0);
    
    new WP_Query([
        'post_type' => 'event',
        'posts_per_page' => 1,
        'post_status' => 'publish',
        'meta_query' => [
            'relation' => 'AND',
            [
                'key' => 'start_date',
                'compare' => '<=',
                'value' => $end->format('Y-m-d'),
                'type' => 'DATE',
            ]
        ],
    ]);
    Plugin Author Marc Bellêtre

    (@marcbelletre)

    Hi guys,

    Sorry for the very long delay, I’ve been quite busy for a while and I couldn’t find time to get back to you.

    @florushj what I told you about the start_date and end_date being set automatically by the plugin was actually wrong. I didn’t remember that I wrote a function that set these values but this function is not part of the plugin. Sorry for the mistake.

    If that can still help here is a function you can use to set these dates when saving a post:

    
    /**
     * Custom actions when an event is saved.
     *
     * @param  int  $post_id
     * @return void
     */
    function my_post_saved($post_id)
    {
        if (!$post_id || get_post_type($post_id) !== 'event') {
            return;
        }
    
        $dates = [];
        $recurrence = get_field('recurrence', $post_id);
    
        foreach ($recurrence['dates_collection'] as $date) {
            if (!in_array($date->format('Y-m-d'), $dates)) {
                $dates[] = $date->format('Y-m-d');
            }
        }
    
        // Sort dates
        sort($dates);
    
        // Update start and end dates in post meta
        update_post_meta($post_id, 'start_date', $dates[0]);
        update_post_meta($post_id, 'end_date', $dates[count($dates) - 1]);
    }
    add_action('acf/save_post', 'my_post_saved');
    

    Hope this helps!

    Thread Starter florushj

    (@florushj)

    Hi Marc, after some fiddling around with the plug-in I have made it work for my use case. Thanks for your help.

    Trying to query and show it all in an agenda style (with individual event entries for recurring events) is still a bit cumbersome.
    Managed to make it work by first querying all events (using WP_query) (normal and recurring ones), and then creating a new events array where normal events and individual event entries for the recurring ones ar pushed into. For recurring events I loop through them and for each individual date I push the post object with date to this events array.

    Plugin Author Marc Bellêtre

    (@marcbelletre)

    Hi @florushj,

    Thank you for the feedback. I’m glad you found a way ??

    I thought you were only looking for a way to query future events. I didn’t understand you were trying to show every date in an agenda style.
    I have worked on multiple projects with the exact same use case and made it work like you did. I could have send you the code I’m using if I knew.

    I know it’s a bit cumbersome but this plugin can only provide a field that you can use on any custom type so the implementation is up to you.

    Maybe if I have some time I could write a small documentation with implementation examples like this one.

    Cheers,
    Marc

    Thread Starter florushj

    (@florushj)

    Hi @marcbelletre,

    I understand. The plug-in itself does a great job. Now I have got this working, it’s exactly what I needed and intuitive to work with. I’ve tried paying it forward by adding the dutch translation for this plug-in.

    I guess adding the agenda listing use case to the documentation would be a very good addition. That and listing all dates for the recurring event are arguably the most common use cases for this plug-in. If you could find the time..

    Thanks for all your help. Cheers!

    Hello @florushj / @marcbelletre,

    I would very much be interested in seeing examples of how you managed to get a list of the recurring events in an agenda-style display.

    Thanks,
    Ray

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Usage in custom WP Query?’ is closed to new replies.