Function to Limit Events By Category

Home Forums Calendar Products Events Calendar PRO Function to Limit Events By Category

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1278186
    Shamsi
    Participant

    Hi,

    I want to write a custom function that will limit the calendar events query to only the results from a specific category.

    Does anyone know how write a WordPress function, such as ‘pre_get_posts’, to limit the events by a category?

    function limit_events_by_category( $query ) {
    /* code to limit the events by a category */
    return $query;
    }
    add_action( 'pre_get_posts', 'limit_events_by_category' );

    (Also, that will not affect any theme custom navigation, thanks)

    #1278196
    Shamsi
    Participant

    I previously posted the question of how to write this function, but now I think I’ve come up with it.

    /**
    * Limit events by a category
    */

    function limit_events_by_category( $query ) {
    if ( ( tribe_is_month() || tribe_is_photo() || tribe_is_week() || tribe_is_list_view() ) && !is_tax() && $query->is_main_query() ) {
    $query->set( 'tax_query', array(
    array(
    'taxonomy' => TribeEvents::TAXONOMY,
    'field' => 'slug',
    'terms' => array('event-category')
    ))
    );
    }
    return $query;
    }
    add_action( 'pre_get_posts', 'limit_events_by_category' );

    #1278781
    Nico
    Member

    Hi there Shamsi,

    Thanks for reaching out to us and for the heads-up on the resolution of the issue 🙂

    The code looks good! Great job!

    I’ll go ahead and close out this thread, but if you need help with anything else please don’t hesitate to create a new one and we will be happy to assist you.

    Best,
    Nico

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Function to Limit Events By Category’ is closed to new replies.