Home › Forums › Calendar Products › Events Calendar PRO › Exclude events from the standard wordpress search results on the frontend
- This topic has 8 replies, 3 voices, and was last updated 6 years, 6 months ago by James O'Sullivan.
-
AuthorPosts
-
September 29, 2017 at 5:44 am #1356675James O’SullivanParticipant
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,
JamesOctober 2, 2017 at 4:54 am #1357633AndrasKeymasterHi 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,
AndrasOctober 14, 2017 at 8:47 am #1363688DennisParticipantI 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
October 16, 2017 at 12:10 pm #1364275AndrasKeymasterHey @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,
AndrasOctober 17, 2017 at 11:50 am #1364873DennisParticipantThanks András:)
October 18, 2017 at 1:27 am #1365183AndrasKeymasterYou are very welcome!
Is there anything else I can help you with before I close this down?
Andras
October 18, 2017 at 11:38 am #1365570DennisParticipantNo more questions from my side, thanks:)
October 19, 2017 at 2:53 am #1365825AndrasKeymasterCool, 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,
AndrasPS: 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!
-
AuthorPosts
- The topic ‘Exclude events from the standard wordpress search results on the frontend’ is closed to new replies.