Hide event category from events page (again)

Home Forums Calendar Products Events Calendar PRO Hide event category from events page (again)

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1137072
    Ruth
    Participant

    Hi there

    I’ve looked through the forums for a way to hide one or more categories from the main /events page, but nothing I’ve found has worked. There doesn’t seem to be any recent suggestions.

    My client uses the calendar for the workshops they conduct (with ticketing) and also offer free listings for community events (no ticketing). They would like to publish two pages, one for each type of event (ie. “in house” and “community”).

    I would like to do this by simply hiding the community events from the /events page, then using the community category to create an additional page. It would be good to also hide the workshop events on the community events page. In effect, two completely separate events pages.

    I use the photo view for listing events.

    Thank you
    Kate

    #1137232
    Nico
    Member

    Howdy Kate,

    Welcome to our support forums and thanks for reaching out to us. I’ll help you here…

    What we need to do here is just hide ‘community’ events from the main ‘events’ page. Once you are view the community category all other categories will automatically excluded, so nothing to do on that front. Paste the snippet below in your theme’s (or child theme’s) functions.php file:


    add_action( 'pre_get_posts', 'exclude_events_category' );
    function exclude_events_category( $query ) {

    if ( $query->query_vars['eventDisplay'] == 'upcoming' || $query->query_vars['eventDisplay'] == 'past' || $query->query_vars['post_type'] == TribeEvents::POSTTYPE && !is_tax(TribeEvents::TAXONOMY) && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'tax_query', array(
    array(
    'taxonomy' => TribeEvents::TAXONOMY,
    'field' => 'slug',
    'terms' => array('excluded-category-slug'),
    'operator' => 'NOT IN'
    )
    ) );
    }

    return $query;

    }

    Be sure to update the 'excluded-category-slug' for the right one in your site.

    Please let me know if this works for you,
    Have a great weekend,
    Nico

    #1137638
    Ruth
    Participant

    Hi Nico

    Unfortunately, that has no effect. I substituted

    ‘field’ => ‘Markets’,
    ‘terms’ => array(‘markets’),

    however, the markets category still appears on the events page:

    http://southernharvest.org.au/events/

    Did I miss something?

    When I view the markets category page, the workshops category (and sub-categories) still appear in the filter bar, which is what I’d like to avoid, if possible.

    http://southernharvest.org.au/events/category/markets/

    Thanks
    Kate

    ps. At this early stage of adding events, I have only created the one community category “markets”, but it will eventually be a sub-category of “community”, just so I don’t confuse you with what I’m describing and how it currently appears.

    #1138046
    Nico
    Member

    Hey Kate,

    Thanks for following up! The code should be the following, you don’t need to change the field value, just add the terms to the array:


    add_action( 'pre_get_posts', 'exclude_events_category' );
    function exclude_events_category( $query ) {

    if ( $query->query_vars['eventDisplay'] == 'upcoming' || $query->query_vars['eventDisplay'] == 'past' || $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && !is_tax(Tribe__Events__Main::TAXONOMY) && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'tax_query', array(
    array(
    'taxonomy' => Tribe__Events__Main::TAXONOMY,
    'field' => 'slug',
    'terms' => array('markets'),
    'operator' => 'NOT IN'
    )
    ) );
    }

    return $query;

    }

    Just to make sure we are on the same page, please note that the code above hides events from the selected slugs in the main events page. This snippet is not intended to work on the filter-bar.

    When I view the markets category page, the workshops category (and sub-categories) still appear in the filter bar, which is what I’d like to avoid, if possible.

    The code below shows how you can hide categories from filter bar for certain category pages, make sure to check if the category slug and names are correct!

    /* Hide categories from filterbar */
    function tribe_hide_filterbar_categories ( $values, $slug ) {

    if ( $slug != 'eventcategory' || ! is_tax( Tribe__Events__Main::TAXONOMY ) ) return $values;

    $cat = get_queried_object();

    if ( 'market' != $cat->name ) return $values;

    foreach ($values as $key => $value ) {

    if( $value['name'] == 'Community' ) {
    unset($values[$key]);
    }
    }

    return $values;
    }

    add_filter( 'tribe_events_filter_values', 'tribe_hide_filterbar_categories', 10, 2 );

    Best,
    Nico

    #1138580
    Ruth
    Participant

    OK. Just going to responde to the first issue and the code you provided at this point.

    That does the trick!

    HOWEVER, it also makes the events completely disappear from the backend. Whether I log in as the administrator in the WordPress backend or I log in as the user from the front end… no event to edit. Nada.

    Sorry.

    I tried to get the second snippet to work, but couldn’t. For now, however, I’d just like to get the first step sorted out and I’ll get back to the filter bar later.

    Thanks
    Kate

    #1138892
    Nico
    Member

    Thanks for following up Kate!

    Let’s add a check to see if we are viewing the admin screen in which case we bail:


    add_action( 'pre_get_posts', 'exclude_events_category' );
    function exclude_events_category( $query ) {

    if ( is_admin() ) return $query;

    if ( $query->query_vars['eventDisplay'] == 'upcoming' || $query->query_vars['eventDisplay'] == 'past' || $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && !is_tax(Tribe__Events__Main::TAXONOMY) && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'tax_query', array(
    array(
    'taxonomy' => Tribe__Events__Main::TAXONOMY,
    'field' => 'slug',
    'terms' => array('markets'),
    'operator' => 'NOT IN'
    )
    ) );
    }

    return $query;

    }

    Please let me know if that fix makes it right in the admin,
    Best,
    Nico

    #1141822
    Ruth
    Participant

    Hi Nico

    Sorry for the long absence. Things got a bit crazy.

    The events now appear in the admin, however are still not appearing in the user’s frontend account to edit. They can add an event, but not edit current events, as they don’t appear.

    Thanks for your help.
    Kate

    #1142662
    Nico
    Member

    Thanks for the feedback Kate! No worries for the delay 🙂

    I amended the script to bail if the user is viewing their event list:

    add_action( 'pre_get_posts', 'exclude_events_category' );
    function exclude_events_category( $query ) {

    if ( is_admin() || !isset( $query->query_vars['eventDisplay'] ) || tribe_is_community_my_events_page() ) return $query;

    if ( $query->query_vars['eventDisplay'] == 'upcoming' || $query->query_vars['eventDisplay'] == 'past' || $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && !is_tax(Tribe__Events__Main::TAXONOMY) && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'tax_query', array(
    array(
    'taxonomy' => Tribe__Events__Main::TAXONOMY,
    'field' => 'slug',
    'terms' => array('markets'),
    'operator' => 'NOT IN'
    )
    ) );
    }

    return $query;

    }

    Give the updated snippet a try and let me know if it works as expected now!

    Best,
    Nico

    #1143563
    Ruth
    Participant

    Hi Nico

    Thank you — users can now access their community events to edit.

    Now to get back to the filter bar customisation.

    I’ve tried adding in that code you provided, but I just can’t get it to have any effect on either the main /events page, nor the “community events” page, located at /events/category/community. Not sure if I’m editing the correct bits, but I’ve tried a few different versions and nothing has made any difference.

    Just to recap: I’d like the Community parent category (and any subcategories) to NOT appear in the filter bar on the main /events page. Then I’d like the Workshops parent category (and any subcategories) to NOT appear in the filter bar on the /events/category/community page.

    I’m trying to effectively creat two discrete events pages.

    Thanks once again
    Kate

    #1143733
    Nico
    Member

    Thanks for confirming Kate! Let’s resolve the filter issue…

    I’d like the Community parent category (and any subcategories) to NOT appear in the filter bar on the main /events page. Then I’d like the Workshops parent category (and any subcategories) to NOT appear in the filter bar on the /events/category/community page.

    So in main events page you need to hide community (and subs) and show the workshops (and subs). On the other hand while viewing the community category page, you want to hide workshops (and subs) and show community (and subs), right?

    If that’s the case then the snippet should be the following:

    /* Hide categories from filterbar */
    function tribe_hide_filterbar_categories ( $values, $slug ) {

    // bail if not event category values
    if ( $slug != 'eventcategory' ) return $values;

    $cat = get_queried_object();

    $hide_cat = '';

    // if we are not on event categry page hide 'community'
    // if we are on community category page hide 'workshops'
    if ( !tribe_is_event_category() ) {
    $hide_cat = 'community';
    } else if ( tribe_is_event_category() && $cat->slug == 'community' ) {
    $hide_cat = 'workshops';
    }

    if ( !empty($hide_cat) ) {
    $hide_term = get_term_by( 'slug', $hide_cat, Tribe__Events__Main::TAXONOMY );
    }

    foreach ($values as $key => $value ) {

    // hide category and subs
    if( $value['data']['slug'] == $hide_cat ) {
    unset($values[$key]);
    } else if ( isset ( $hide_term ) ) {
    $term = get_term_by( 'id', $value['value'], Tribe__Events__Main::TAXONOMY );

    if ( $term->parent == $hide_term->term_id ) {
    unset($values[$key]);
    }
    }

    }

    return $values;
    }

    add_filter( 'tribe_events_filter_values', 'tribe_hide_filterbar_categories', 10, 2 );

    The code above assumes the event category slugs are ‘community’ and ‘workshops’, so be sure to check that’s correct!

    Please let me know if this works for you,
    Best,
    Nico

    #1144993
    Ruth
    Participant

    That is perfect and awesome and all those sorts of good things.

    Thank you so much for all your help!

    Kate

    #1145203
    Nico
    Member

    Woa! Stocked to hear we could sort this out Kate 🙂

    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 12 posts - 1 through 12 (of 12 total)
  • The topic ‘Hide event category from events page (again)’ is closed to new replies.