Exclude Venues and Organizers from WordPress Search

Home Forums Calendar Products Events Calendar PRO Exclude Venues and Organizers from WordPress Search

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1154055
    Kevin
    Participant

    Is there any way (with a built in setting or additional code) to exclude venus and organizers from the wordpress search.

    Given that these two CPTs aren’t related directly to my organization I’d like them not to show up in the wordpress search

    #1154381
    Geoff
    Member

    Hi Kevin,

    Happy Monday and hope you had a great weekend!

    You should be able to exclude specific post types from search using a function much like the one outlined in this post.

    The two post types you want to do this for are:

    • tribe_organizer
    • tribe_venue

    Cheers!
    Geoff

    #1154542
    Kevin
    Participant

    Ya, I caught that post and have tried it, even for just one of those CPTs but no luck.

    Tried with a clean install of wordpress, base theme and the plugin but no luck.

    Hmmm.

    #1154587
    Geoff
    Member

    Hey Kevin!

    I just stumbled across the Simply Exclude plugin which might be a good alternative. It will allow you to specify specific post types to exclude from the search action.

    Will that work, by chance?

    Cheers!
    Geoff

    #1155269
    Kevin
    Participant

    Ideally I’d like to not use an additional plugin to avoid all of the potential issues and bloat that comes along with it.

    With that said, this isn’t working either, but it doesn’t work for my other CPTs either.

    I’ll have to do some additional digging, but at the moment the only way for me to effectively exclude CPTs from searches is to include 'exclude_from_search' => true, initially when the CPT is declared and not through other means. This works for other CPTs I have control over.

    #1155310
    Geoff
    Member

    initially when the CPT is declared and not through other means. This works for other CPTs I have control over.

    Yeah, that’s what I was hoping to accomplish with the filter from my first reply. I gave that a shot on my test site and it seems to do the trick.

    Another way to go about it would be to write a filter for search itself, like this:

    function exclude_from_search($query) {
    if ($query->is_search) {
    $query->set('post_type', 'post');
    }
    return $query;
    }
    add_filter('pre_get_posts','exclude_from_search');

    That will return only default WordPress posts in search.

    Does that work for you as well?

    Thanks!
    Geoff

    #1155558
    Kevin
    Participant

    Alright, so here where I’m at.

    Your last response, to modify the search query, works but it’s not ideal. Rather than limiting what’s search I have to state what’s included. Could become a pain depending on the number of post types to keep included in the search. It’s an option though. So Thank You!

    But…

    Back to your initial suggestion, this function:

    add_action( 'init', 'update_my_custom_type', 99 );
    
    function update_my_custom_type() {
    	global $wp_post_types;
     
    	if ( post_type_exists( 'my-custom-type' ) ) {
     
    		// exclude from search results
    		$wp_post_types['my-custom-type']->exclude_from_search = true;
    	}
    }

    I have it functioning for my CPTs. I think it was a cache problem initially of why it wasn’t working.

    With that said, it’s still not working for tribe_venue* and I think I found the issue:

    on line 1396 of this file /wp-content/plugins/events-calendar-pro/src/Tribe/Main.php the following exists:

    $venue_args['exclude_from_search'] = false;

    It appears as if that line is taking precedent over the original function suggested. If I modify that line to include true as the option it will exclude the search results, obviously.

    Is there a way to include a modified Main.php file in my template folder (like I would have with files to override the standard calendar layout) so that upon plugin update I can keep any modifications I need to make to line 1396 in that file? If so, what might the folder structure look like?

    * note: it appears that this was never an issue for tribe_organizer, I was simply including that in the original post because I assumed it worked the same way.

    #1155798
    Geoff
    Member

    Hey Kevin,

    I brought in another pair of eyes to look at this and this function should do the trick:

    /**
    * If The Events Calendar is active, restores the default exclude_from_search
    * property of the venue post type.
    */
    function disable_public_venue_search() {
    if ( ! class_exists( 'Tribe__Events__Main' ) ) return;
    $tec = Tribe__Events__Main::instance();
    $venue_args = $tec->getVenuePostTypeArgs();
    $venue_args['exclude_from_search'] = true;
    register_post_type( Tribe__Events__Main::VENUE_POST_TYPE, apply_filters( 'tribe_events_register_venue_type_args', $venue_args ) );
    }
    
    // ECP enables search for venues during wp_loaded:10 so we need this to run just after that
    add_action( 'wp_loaded', 'disable_public_venue_search', 50 );

    You’re right that organizers are excluded by default. This will make sure the same is true of venues.

    Let me know if this helps do the trick!

    Geoff

    #1156876
    Kevin
    Participant

    Thanks for taking the time to put this together. All seems to be working now. I appreciate your help with this.

    #1157055
    Geoff
    Member

    My pleasure! I’m so glad to hear all is working well and I appreciate you following up to let me know. 🙂

    I’ll go ahead and close this thread but please feel free to let us know if any other questions come up and we’d be happy to help.

    Cheers!
    Geoff

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘Exclude Venues and Organizers from WordPress Search’ is closed to new replies.