Exclude events from the standard wordpress search results on the frontend

Home Forums Calendar Products Events Calendar PRO Exclude events from the standard wordpress search results on the frontend

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1356675
    James O’Sullivan
    Participant

    Hi there, I already opened up this support request a while back here https://theeventscalendar.com/support/forums/topic/exclude-events-from-the-wordpress-search-results-on-the-frontend/ and I got a handy snippet to insert which actually does work on the frontend however there are some bad side effects. Basically by adding the snippet outlined when you are in the backend of the site and you search for an event in the backend here: http://recordit.co/eS4FhLUB6X or even if you search for a page in the pages area the results do not return anything here.

    So basically there is a conflict with this. Any ideas on this one? is there an edit to the snippet that does not mess up the search results in the backend also?

    Thanks in advance.

    Thanks,
    James

    #1357633
    Andras
    Keymaster

    Hi James,

    You could do a check whether you are on the back-end or not, and if yes, then skip the whole thing.

    Something like this:

    function exclude_from_search($query) {
    // skip if on back-end
    if ( is_admin() ) return $query;
    
    // modify search
    if ($query->is_search) {
    $query->set('post_type', 'post');
    }
    return $query;
    }
    add_filter('pre_get_posts','exclude_from_search');

    Note, this hasn’t been tested, but that’s basically the logic of it.

    Let me know if this does the trick.

    Cheers,
    Andras

    #1363688
    Dennis
    Participant

    I looked for the same thing and it looks like this works almost fine.
    Only thing missing is the pages. The search results page only shows the posts with this. Can you tell me how to include pages too? So that the search is active for posts and pages only.

    Thanks

    #1364275
    Andras
    Keymaster

    Hey @theedge,

    Thanks for your question.

    To include pages or any other post type you will need to do a small modification only. Change this:

    $query->set( 'post_type', 'post' );

    to this:

    $query->set( 'post_type', array( 'post', 'page', 'album', 'movie', 'quote' ) );

    That one includes other post types as well. You will only need ‘post’ and ‘page’ in the array to get the result you need.

    Cheers,
    Andras

    #1364873
    Dennis
    Participant

    Thanks András:)

    #1365183
    Andras
    Keymaster

    You are very welcome!

    Is there anything else I can help you with before I close this down?

    Andras

    #1365570
    Dennis
    Participant

    No more questions from my side, thanks:)

    #1365825
    Andras
    Keymaster

    Cool, then I am going to close this ticket, but if you need anything else related to this topic or another please create a new ticket and we’ll be happy to help.

    Cheers,
    Andras

    PS: If you like our plugins, and you didn’t yet do so 🙂 we would be happy to receive a review in the wordpress.org repository. Thanks!
    https://wordpress.org/support/view/plugin-reviews/the-events-calendar/
    https://wordpress.org/support/view/plugin-reviews/event-tickets/

    PS2: We’d be also grateful if you would give us feedback on your satisfaction with support. Just click on one of the classy looking emojis below. 🙂 If you can spare a few words, that’s even better. Doublethanks!

     

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Exclude events from the standard wordpress search results on the frontend’ is closed to new replies.