Related Events

Home Forums Calendar Products Events Calendar PRO Related Events

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #967534
    Michael McFann
    Participant

    Where can I find an explanation of how related events works? I do not see any fields that would seem to indicate they are used for this function. All I see is a way to turn on/off the display of related events.

    Thanks – Michael

    #967697
    Brian
    Member

    Hi Michael,

    The related events look for events with the same tags and/or categories.

    We use the wp_get_post_tags and wp_get_object_terms to get tags and categories of the current events

    Then we use those and the orderby random argument to find related events.

    Here is the entire function in 3.10:

    /**
    * Template tag to get related posts for the current post.
    *
    * @param int $count number of related posts to return.
    * @param int|obj $post the post to get related posts to, defaults to current global $post
    *
    * @return array the related posts.
    */
    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 ) ;
    }

    Let me know if you have any follow up questions.

    Thanks

    #969301
    Michael McFann
    Participant

    Thanks for that info.

    We are using the The Events Calendar Category Colors so we are wondering if there is a way to remove the use of categories for related events and just use tags?

    Thanks – Michael

    #969422
    Brian
    Member

    Hi,

    The only way to remove the event categories would be to replace that function ( tribe_get_related_posts() ) with your own and then put that into the Event Templates following our themer’s guide:

    https://theeventscalendar.com/knowledgebase/themers-guide/

    So you could take that function, add it to your theme’s functions.php, rename it. Then remove the category part of the function and add it in place of the current related events function.

    That function is called on this template:

    events-pro\src\views\pro\related-events.php

    In 3.9 the template is here:

    events-pro\views\pro\related-events.php

    All you have to do is change the name of the function here to what you call it:

    $posts = tribe_get_related_posts();

    Cheers

    #984322
    Support Droid
    Keymaster

    This 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.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Related Events’ is closed to new replies.