Search by category doesn't work - Acme Themes

Best Premium and Free WordPress Themes Forums Online Shop Search by category doesn't work

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #34065
    valeriuz
    Participant

    Hello,

    I’ve noticed that selecting a category on the front page, for search, doesn’t change the search results. The search only checks for the keywords inserted in the search field, and doesn’t look at the selected category.

    Can you tell me how to fix that ? I saw this happens also on the test site you preview for the online shop theme.

    Thank you.

    #34090
    acmethemes
    Keymaster

    Dear Valeriuz,
    By WooCommerce default, the search field will only work via keywords. Though, it can be changed through theme customization.
    Best Regards!

    #43175
    ianstrack
    Participant

    Hello, I have the same issue. Can you please describe how to fix it? Love the theme so far. Thanks!

    #43176
    ianstrack
    Participant

    I found this one bit of code but it doesn’t work…

    https://gist.github.com/saadwaseem/241f11293c0c2902365ef23894f27ff9

    #43203
    ianstrack
    Participant

    I added this to the functions.php under Appearance>Editor and it solved all my trouble. It allows the search to use the categories, and it excludes any out of stock items, as well as displays the search results correctly.

    /**
    * Adds Category taxonomy query to WP search query
    * This addition will make the search more relevant
    * @param type $query
    */
    function search_filter($query) {
    if($query->is_search()) {
    // category terms search.
    if (isset($_GET[‘product_category’]) && !empty($_GET[‘product_category’])) {
    $tax_query = array(
    ‘taxonomy’ => ‘product_cat’,
    ‘terms’ => array($_GET[‘product_category’]),
    ‘field’ => ‘id’,
    ‘operator’ => ‘IN’,
    );
    $query->tax_query->queries[] = $tax_query;
    $query->query_vars[‘tax_query’] = $query->tax_query->queries;
    }
    }
    }

    add_action(‘pre_get_posts’,’search_filter’);

    add_action( ‘pre_get_posts’, ‘iconic_hide_out_of_stock_products’ );

    function iconic_hide_out_of_stock_products( $q ) {

    if ( ! $q->is_main_query() || is_admin() ) {
    return;
    }

    if ( $outofstock_term = get_term_by( ‘name’, ‘outofstock’, ‘product_visibility’ ) ) {

    $tax_query = (array) $q->get(‘tax_query’);

    $tax_query[] = array(
    ‘taxonomy’ => ‘product_visibility’,
    ‘field’ => ‘term_taxonomy_id’,
    ‘terms’ => array( $outofstock_term->term_taxonomy_id ),
    ‘operator’ => ‘NOT IN’
    );

    $q->set( ‘tax_query’, $tax_query );

    }

    remove_action( ‘pre_get_posts’, ‘iconic_hide_out_of_stock_products’ );

    }

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