-
AuthorPosts
-
December 23, 2020 at 7:19 am #1269471
Hi,
On the Enfold menu search is it possible to filter it so it only searches certain taxonomys e.g. it only searches posts?
Thanks,
DannyDecember 23, 2020 at 11:00 am #1269496Hi Danny,
Add these codes in functions.php:
For search results page:
function exclude_pages_from_search($query) { if ( $query->is_main_query() && is_search() ) { $query->set( 'post_type', 'post' ); } return $query; } add_filter( 'pre_get_posts','exclude_pages_from_search' );
For dropdown search result:
function avf_modify_ajax_search_query($search_parameters) { parse_str($search_parameters, $params); $params['post_type'] = 'post'; $search_parameters = http_build_query($params); return $search_parameters; } add_filter('avf_ajax_search_query', 'avf_modify_ajax_search_query', 10, 1);
Hope it helps :)
Best regards,
NikkoDecember 24, 2020 at 10:28 am #1269728Hi
Thanks for that, do I need to add both sections, what is the dropdown search results?
Thanks
DannyDecember 27, 2020 at 4:57 pm #1269866Hi Danny,
Yes, you need both. The dropdown is the search results that come like a preview to a search field in your header.
Best regards,
VictoriaJanuary 10, 2021 at 9:19 am #1271315Hi
Thanks this has worked but one thing I have noticed now is in admin when searching pages for example, it brings up posts as results now, is there a way round this?
Thanks,
DannyJanuary 11, 2021 at 5:04 am #1271472Hi Danny,
Do you want to give different results depending on the user role?
If yes, then you can use if-else conditions then base it on Roles and Capabilities: https://wordpress.org/support/article/roles-and-capabilities/
Otherwise, you need to filter it based on the page where the user is on then use Conditional Tags: https://codex.wordpress.org/Conditional_TagsBest regards,
NikkoJanuary 12, 2021 at 8:30 am #1271775Hi,
Sorry I might not have worded right, what I mean is the search on the front end is working fine now. The issue we have now is that this has also changes the search in the backend/admin area so when you go into the pages section where you would add a page and search for something in there, that is also only bringing posts up to edit instead of pages, I wasn’t expecting this to happen,I just wanted the front end search for the user to only search posts.
Thanks,
DannyJanuary 13, 2021 at 8:21 am #1272060Hi Danny,
I see, please replace the codes I gave with the following:
For search results page:
function exclude_pages_from_search($query) { if ( $query->is_main_query() && is_search() && !is_admin() ) { $query->set( 'post_type', 'post' ); } return $query; } add_filter( 'pre_get_posts','exclude_pages_from_search' );
For dropdown search result:
function avf_modify_ajax_search_query($search_parameters) { if ( !is_admin() ) { parse_str($search_parameters, $params); $params['post_type'] = 'post'; $search_parameters = http_build_query($params); } return $search_parameters; } add_filter('avf_ajax_search_query', 'avf_modify_ajax_search_query', 10, 1);
Best regards,
NikkoJanuary 15, 2021 at 8:30 am #1272611Hi
Thanks for that, it’s nearly worked, the search is fine now in admin and on the results page when not logged in brings up only posts so that is great, on the dropdown results though it is now brining up all the taxonmys now, is there someone that needs tweaking in that part?
Thanks,
DannyJanuary 15, 2021 at 2:25 pm #1272731Hi Danny,
Can you give us a link to your site? and also some keywords to search to test this.
Best regards,
NikkoJanuary 16, 2021 at 7:55 am #1272880Hi,
Yes no problem, see info in the box.
Thanks,
DannyJanuary 18, 2021 at 3:59 am #1273195Hi Danny,
Thanks, can you try to remove the conditional statement from this code I gave:
function avf_modify_ajax_search_query($search_parameters) { if ( !is_admin() ) { parse_str($search_parameters, $params); $params['post_type'] = 'post'; $search_parameters = http_build_query($params); } return $search_parameters; } add_filter('avf_ajax_search_query', 'avf_modify_ajax_search_query', 10, 1);
so it should be (just like the previous):
function avf_modify_ajax_search_query($search_parameters) { parse_str($search_parameters, $params); $params['post_type'] = 'post'; $search_parameters = http_build_query($params); return $search_parameters; } add_filter('avf_ajax_search_query', 'avf_modify_ajax_search_query', 10, 1);
Then test if it works properly, then also test if it affects the backend.
Best regards,
NikkoJanuary 18, 2021 at 8:07 am #1273255That’s done the job thank you :)
January 18, 2021 at 9:08 am #1273264Hi dannyl82,
We’re glad to hear that :)
Thanks for using Enfold and have a great day!Best regards,
Nikko -
AuthorPosts
- The topic ‘Menu Search Only Posts’ is closed to new replies.