Home › Forums › Calendar Products › Events Calendar PRO › How are Related Events determined?
- This topic has 8 replies, 3 voices, and was last updated 9 years, 6 months ago by
Sean.
-
AuthorPosts
-
September 28, 2016 at 2:08 pm #1170053
Sean
ParticipantHi,
Hoping you can help us understand the Related Events section that we can have included on the single event pages. What is the default algorithm or query used to display events there? What determines which events are “related” to the event the user is currently viewing? Is there anything that determines how one “related” event might rank higher than another “related” event, or are X number of related events randomly pulled from all of the Related Events?
To give a better understanding on why we’re asking, we currently don’t have Related Events appearing on our site. However, depending on what the default logic is and how the Related Events might be able to be customized (by a function), we wanted to see what is possible “out of the box” and if there are any examples of functions showing how we can customize the logic (e.g limit related events to only show events from the same [custom] taxonomy and the same state).
Thank you,
KarlySeptember 28, 2016 at 4:14 pm #1170120George
ParticipantHey @Karly!
The related posts are retrieved using a very simple set of parameters: tags and Event Categories.
You can explore the complete set of code by heading to this file in Events Calendar Pro:
/events-calendar-pro/src/functions/template-tags/general.phpIn there you’ll find the tribe_get_related_posts() function, which is as follows:
function tribe_get_related_posts( $count = 3, $post = false ) {
$post_id = Tribe__Events__Main::postIdHelper( $post );
$tags = wp_get_post_tags( $post_id, array( 'fields' => 'ids' ) );
$categories = wp_get_object_terms( $post_id, Tribe__Events__Main::TAXONOMY, array( 'fields' => 'ids' ) );
if ( ! $tags && ! $categories ) {
return;
}
$args = array(
'posts_per_page' => $count,
'post__not_in' => array( $post_id ),
'eventDisplay' => 'list',
'tax_query' => array( 'relation' => 'OR' ),
'orderby' => 'rand',
);
if ( $tags ) {
$args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $tags );
}
if ( $categories ) {
$args['tax_query'][] = array(
'taxonomy' => Tribe__Events__Main::TAXONOMY,
'field' => 'id',
'terms' => $categories,
);
}$args = apply_filters( 'tribe_related_posts_args', $args );
if ( $args ) {
$posts = Tribe__Events__Query::getEvents( $args );
} else {
$posts = array();
}return apply_filters( 'tribe_get_related_posts', $posts );
}
When it comes to changing the parameters used for finding “related” events, you can use the tribe_related_posts_args filter seen in the code above. This filter lets you filter the query arguments used in that eventual Tribe__Events__Query::getEvents( $args ) call in the function above.
☝️ I hope this information helps!
Sincerely,
GeorgeSeptember 28, 2016 at 9:01 pm #1170203Sean
ParticipantHi George,
As always, thanks for the info on this. It does help get me on the right path!
Just to make sure I’m understanding this correctly, could you please confirm a few things?
By default, the related posts display 3 random events that have the same tags or event categories as the event currently being viewed? Related events are not ranked higher or lower than other related events (e.g. if an event has two of the same event categories AND one or more of the same terms in its event name, it won’t appear before an event that has only one matching event category and no common terms)?
Also, I found the following snippet from Brook that may be helpful in guiding me. Does this filter (and example) add the two month time frame to the existing tags and event categories parameters, or does it replace the original parameters? In other words, will it show only events occurring within 2 months of the current event’s date, or will it show events occurring within 2 months of the current event’s date that also have one or more of the same tags and/or event categories?
Thank you,
Karly<?php /* * Limits Related Posts to only show events within 2 months of the current events dates */ function tribe_related_posts_args_limiter ( $args = array() ) { global $post; $event_start_date = strtotime( $post->EventStartDate ); $event_end_date = strtotime( $post->EventEndDate ); $date_format = Tribe__Events__Date_Utils::DBDATETIMEFORMAT; $args['eventDisplay'] = 'custom'; $args['start_date'] = date_i18n( $date_format, strtotime( '-2 month', $event_start_date ) ); $args['end_date'] = date_i18n( $date_format, strtotime( '+2 month', $event_end_date ) ); return $args; } add_filter( 'tribe_related_posts_args', 'tribe_related_posts_args_limiter', 10, 1 );September 29, 2016 at 7:03 am #1170338George
ParticipantHey Karly,
You are correct in that there is no “ranking” of related events; no default calculating of, for example, “this has 2 categories similar, so it’s MORE related than this other event with only 1 category similar.”
You are also correct with the second description you used for that code snippet shared by brook:
or will it show events occurring within 2 months of the current event’s date that also have one or more of the same tags and/or event categories?
☝️ Yes, it applies the +/- 2-month limitation to the existing tag-and-category filters. So if there is an event within 2 months of the event date but it has no common tags or categories, it will NOT show up in the “related” events.
I hope this helps!
GeorgeSeptember 29, 2016 at 6:12 pm #1170806Sean
ParticipantHey George,
Okay thanks for confirming! One last question. In my case, I’d like to replace the existing Tag and Event Category filter with my custom taxonomy. So if there is an event with a common value from my custom taxonomy, I’d like it to display in the related events (regardless of any other variables such as tags, etc).
Would using the tribe_related_posts_args filter in a function mean the custom code would replace the default query that uses tags and event categories? Meanwhile, using the tribe_related_posts_args_limiter function will add those “rules” to the existing tag and/or event categories criteria?
Just want to make sure I’m grasping how these work prior to attempting to replace the tag/categories with my custom taxonomy, as I’m certainly a novice when it comes to php 🙂
– Karly
-
This reply was modified 9 years, 7 months ago by
Sean.
October 3, 2016 at 9:36 am #1171828George
ParticipantGood question, Karly!
tribe_related_posts_args_limiter() is not actually a filter you can use.
tribe_related_posts_args_limiter() is the callback function in the snippet above—meaning that the code is basically saying, “when the tribe_related_posts_args filter happens, run the function called tribe_related_posts_args_limiter.”
That tribe_related_posts_args_limiter() function in the snippet above is what modifies the various query arguments in the related events function.
So, if you were trying to modify how related events were found, you would write your own callback function like this:
function karlys_custom_related_events_function( $args = array() ) {// Get the Event ID.
$post_id = get_the_ID();// Get the existing terms of the custom taxonomy on the event.
$event_terms = wp_get_object_terms( $post_id, 'karlys_custom_taxonomy', array( 'fields' => 'ids' ) );// Reset the tax query so that it's not looking for tags and event cats.
$args['tax_query'] = array( 'relation' => 'OR' );// Make a new tax query saying, "get other events with the same custom taxonomy terms as this one."
$args['tax_query'][] = array(
'taxonomy' => 'karlys_custom_taxonomy',
'field' => 'id',
'terms' => $event_terms,
);// Return the filtered arguments.
return $args;
}add_filter( 'tribe_related_posts_args', 'karlys_custom_related_events_function', 10, 1 );
☝️ This code example also demonstrates modifying the taxonomy parameters to use a custom taxonomy like you mentioned. 😉 It’ll take some tinkering, but I hope this serves as a helpful “template” for your customizations here!
Cheers,
GeorgeOctober 17, 2016 at 1:36 pm #1177924Sean
ParticipantHi George,
Sorry for the late follow up here. Thank you so much for the additional info and this “template” for modifying how related events are found. As always, really appreciate the help!
Best regards,
KarlyOctober 17, 2016 at 9:04 pm #1178135George
ParticipantSure thing! 😀
Cheers,
George -
This reply was modified 9 years, 7 months ago by
-
AuthorPosts
- The topic ‘How are Related Events determined?’ is closed to new replies.
