How to loop through an event's reucrring instances with wp_query

Using wp_query I was able to loop through all parent level recurring events.. but how do I loop through the child-instances of each parent?

$args = array (
	'post_type'              => array( 'incsub_event' ),
	'post_status'            => array( 'publish' ),
	'posts_per_page'         => '-1',
);
$query = new WP_Query( $args );  // this only produces parent-level events as well as detached events.

I want loop through all recurring instances and display custom field data for each. (All instances within a specific time frame.)

  • Kasia Swiderska

    Hello Nanjo,

    WP_Query won't really be suitable for this with events, you'd be best to use the events class of Eab_AllRecurringChildrenCollection (or they're all in the lib/class_eab_collection.php :slight_smile:

    Or alternatively, for upcoming events, you can use Eab_CollectionFactory::get_upcoming_events() to get the upcoming events and then check/call for is_recurring to check if it's a recurring event.

    Kind regards,
    Kasia

  • Nanjo

    Hi, Kasia

    1) Can you post or link to a complete example of how to make use of this class "Eab_AllRecurringChildrenCollection"?

    2) You say wp_query isn't suitable but I just noticed that switching the post status of the query to "ANY" instead of "PUBLISH" seems to produce all the events recurring and children. It seems like a direct solution when combined with a check in the loop but I'm curious if there is a reason why this wouldn't be desirable, could you ask a tech if this is safe to use:

    $args = array (
    	'post_type'              => array( 'incsub_event' ),
    	'post_status'            => array( 'any' ),
    	'posts_per_page'         => '-1',
    );
    $query = new WP_Query( $args );  // this only produces parent-level events as well as detached events.

    Then, in the loop to eliminate the recurring/parent event this seems to work:

    $event = new Eab_EventModel($post);
    if (!$event->is_recurring_child()) continue;

    But as you said, this may not be suitable for some reason. I would like to know for sure if this is a less than desirable method for any reason.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.