Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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?

    #1471357

    Hey 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,
    Ismael

    #1471371

    Hi 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

    #1471373

    Hi,

    We adjusted the filter a bit. Please try it again and let us know of the result.

    Best regards,
    Ismael

    #1471374

    Hi 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

    #1471377

    Hi,

    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,
    Ismael

    #1471396

    i 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 password

    function 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 ?

    #1471398

    by 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

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.