Tagged: search ajax hidden products
-
AuthorPosts
-
November 14, 2024 at 11:16 am #1471307
I created a search form in the Advanced Layout Builder. I have two options: show te results on the same page with AJAX or redirect to the search results page.
On redirection, everything works fine. But when I choose to show results on same page: also hidden products are shown in the results and I don’t want that. I don’t see the option to exclude those items from the search results in the Search Bar settings in ALB.
The hidden products themselves are actually set to “hidden” and not visible in search results and shop.
I cleared/flushed all caches, did a rebuild of the search index and also temporary fully disabled the cache.
Do you have any ideas how to fix this?
November 15, 2024 at 6:40 am #1471357Hey Buskruit Internet,
Thank you for the inquiry.
Please try to add this filter in the functions.php file to exclude hidden products from the AJAX search:
function avf_exclude_hidden_products_from_ajax_search( $search_query ) { parse_str( $search_query, $search_parameters ); $search_parameters['meta_query'][] = array( 'key' => '_visibility', 'value' => 'hidden', 'compare' => '!=' ); return http_build_query( $search_parameters ); } add_filter( 'avf_ajax_search_query', 'avf_exclude_hidden_products_from_ajax_search' );
Best regards,
IsmaelNovember 15, 2024 at 8:39 am #1471371Hi Ismael,
Thanks for your reply. After applying this filter, there are no search results anymore at all (with AJAX). After hitting the enter-button, the correct search results are shown. Do you have any ideas?
Best regards,
Gert
November 15, 2024 at 8:46 am #1471373Hi,
We adjusted the filter a bit. Please try it again and let us know of the result.
Best regards,
IsmaelNovember 15, 2024 at 8:54 am #1471374Hi Ismael,
With this filter, also no search results anymore. But I also asked ChatGPT for help and did some research in the database. Probably looking for _visibility isn’t the correct way, but looking for the taxonomy product_visibility did help me. This is my final, working filter:
function avf_exclude_hidden_products_from_ajax_search( $search_query ) { parse_str( $search_query, $search_parameters ); $search_parameters['tax_query'][] = array( 'taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => array( 'exclude-from-catalog', 'exclude-from-search' ), 'operator' => 'NOT IN', ); return http_build_query( $search_parameters ); } add_filter( 'avf_ajax_search_query', 'avf_exclude_hidden_products_from_ajax_search' );
For me it seems to work correctly now, or do you have any other thoughts on this code?
Best regards,
Gert
November 15, 2024 at 9:13 am #1471377Hi,
Great! Glad to know that you managed to fix it. Please feel free to open another thread if you have more questions about the theme.
Have a nice day.
Best regards,
IsmaelNovember 15, 2024 at 11:31 am #1471396i got an additional solution – even if you like to hide some special posts :
it limits the results to 5 posts – and excludes the post-id’s 68 and 55 but shows only published posts – and with no passwordfunction avia_filter_ajax_search_results( $search_parameters ){ $defaults = array('numberposts' => 5, 'post_type' => array( 'post', 'portfolio', 'page' ), 'post__not_in' => array(68,55), 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false); $_REQUEST['s'] = apply_filters( 'get_search_query', $_REQUEST['s']); $search_parameters = array_merge( $defaults, $_REQUEST ); return $search_parameters; } add_filter('avf_ajax_search_query', 'avia_filter_ajax_search_results', 10, 1);
by the way – would it be possible to limit not the whole results to 5 – but for each post_type ?
November 15, 2024 at 12:13 pm #1471398by the way : to influence the search query – to include for example attachments you can do :
code snippet removed – because it does not work as expected. – sorry
-
AuthorPosts
- You must be logged in to reply to this topic.