Home › Forums › Calendar Products › Events Calendar PRO › Issues w/ getting WP_Query() to return only first instance of recurring events
- This topic has 5 replies, 3 voices, and was last updated 10 years, 1 month ago by
Support Droid.
-
AuthorPosts
-
February 12, 2016 at 5:54 pm #1072314
integrity
ParticipantI am trying to use WP_Query() to get a list of 4 events that are upcoming and belong to the “featured” category I created.
This query seems to work great when I only mark non-recurring events into the “featured” category, but as soon as I mark one or more recurring events as having the “featured” category I get back all occurrence of the event.
$args = array(
‘posts_per_page’ => 4,
‘post_status’ => ‘publish’,
‘post_type’ => ‘tribe_events’,
‘orderby’ => ‘date’,
‘order’ => ‘ASC’,
‘meta_query’ => array(
array(
‘key’ => ‘_EventStartDate’,
‘value’ => date(‘Y-m-d H:i:s’),
‘compare’ => ‘>=’,
‘type’ => ‘DATETIME’
)
),
‘tax_query’ => array(
array(
‘taxonomy’ => ‘tribe_events_cat’,
‘field’ => ‘slug’,
‘terms’ => ‘featured’
))
);
`I need a solution that allows me to only receive back only the first instance of a recurring event + any other recurring or nonrecurring events that are in the “featured” category, up to 4 total.
I did notice that the first occurrence of a recurring event in the database has the parent_post value set to “0”, but adding “‘post_parent’ => 0,” to the query still does not seem to return the right results.
Please help!
February 15, 2016 at 6:02 am #1073000George
ParticipantHey @integrity,
We unfortunately cannot help with questions like this because it relates to your own custom coding endeavors 🙁
I’m sorry to bear that news! Please see the “Product Support” section of this page for more information → http://theeventscalendar.com/terms
I will unfortunately have to close up this thread.
Thank you,
GeorgeFebruary 15, 2016 at 6:11 am #1073202George
ParticipantNow, while the above disclaimers are true, I did want to try and help a little bit before having to close up this thread!
There are unfortunately some tricky things with “recurring events” that makes querying and such a bit challenging.
With this in mind, I would recommend using the wrapper function tribe_get_events() instead of WP_Query directly. You can learn more about tribe_get_events() here → https://theeventscalendar.com/knowledgebase/using-tribe_get_events/
Now, one thing I find works well here is to use the post_parent__in parameter to filter events by post_parent, instead of post_parent directly.
I cannot admit to fully knowing why this works more effectively – but for me, it works very well and limits results to just top-level / parent Recurring Events; the first instance of a recurring event, in other words.
So, for example, this code for me works effectively to get the next 25 upcoming events of the ‘featured’ Event Category, and only the top-level event, so it only returns standalone events OR the first instance of a recurring event. But none of the “child” events in a recurring series:
$events = tribe_get_events( array(
'posts_per_page' => 25,
'start_date' => current_time( 'Y-m-d' ),
'eventDisplay' => 'custom',
'post_parent__in' => array( 0 ),
'tax_query' => array(
array(
'taxonomy' => 'tribe_events_cat',
'field' => 'slug',
'terms' => 'featured'
)
)
) );
Does this work well for you?
If you don’t need “upcoming” events and just need all events in general, check out the article I shared above about how to use the ‘start_date’ parameter and such to get different results for that.
I hope this information helps!
Sincerely,
GeorgeFebruary 24, 2016 at 5:03 pm #1081793integrity
ParticipantThank you again George for your reply and recommendations.
I am more then happy to use your “tribe_get_events” WP_Query wrapper to get the desired results.
It does appear your recommendation is working better than my previous attempt using WP_Query. Thank you for your help with that.
One thing I am interested in is better understanding how your tribe_get_events wrapper works. To be frank, your auto generated documentation for this function (and nearly all other functions you expose) is less than desireable and hardly describes what query options are available. Is there somewhere or someone I can refer to to learn more about how to use this function?
Thanks for all your help!
February 25, 2016 at 3:36 pm #1082237George
ParticipantHey @Integrity,
Thanks for your follow-up, I’m glad the function is serving you well! When it comes to learning more about it, there are two excellent resources:
• This knowledgebase article → https://theeventscalendar.com/knowledgebase/using-tribe_get_events/
• The function itself. The function is defined at this location in your plugin files:
the-events-calendar/src/Tribe/Query.phpThe function in question, specifically, is getEvents() in this file. It’s around line 965.
Reading this function is a great way to see its inner workings, and since it uses a lot of WordPress core functions itself, you can then research each aspect of the function if you’re not already familiar with it.
I hope that’s helpful!
Thanks,
GeorgeMarch 11, 2016 at 8:35 am #1087777Support Droid
KeymasterThis topic has not been active for quite some time and will now be closed.
If you still need assistance please simply open a new topic (linking to this one if necessary)
and one of the team will be only too happy to help. -
AuthorPosts
- The topic ‘Issues w/ getting WP_Query() to return only first instance of recurring events’ is closed to new replies.
