WooCommerce product search titles only

I had asked this earlier and solved it using the Relevanssi plugin, however that plugin is now breaking things so I can’t use it anymore. How is something so freaking simple made so impossible by woocommerce?? AAARG!

I tried using this solution, but it didn’t seem to work for me. I’ve also tried searching out the woocommerce product_search function and adding a filter to that, but didn’t get anywhere with it, either.

Read More

Here’s the last thing I tried, which still returned search results from all content, not just titles:

MarkUp

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <input type="hidden" name="post_type" value="product" />
    <input type="text" value="" name="s" />
    <input type="submit" value="Search" />
</form> 

Search-SQL filter callback

// Search product titles only.
function __search_by_title_only( $search, &$wp_query ) {
    global $wpdb;
    if($_GET['post_type'] = 'product' )
        return $search;
    $q = $wp_query->query_vars;
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search =
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if ( ! empty( $search ) ) {
        $search = " AND ({$search}) ";
        if ( ! is_user_logged_in() )
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
}
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

Anyone? I imagine it has something to do with the woocommerce post type itself… they like to put in a lot of custom hooks and such.

Related posts

2 comments

  1. This is an 9 months question, but since I had the same issue and found a solution, I came here to post it.

    On file wp-content/plugins/woocommerce/classes/class-wc-query.php, function pre_get_posts( $q ), WooCommerce defines at line 114:

    add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
    

    This is the point when WooCommerce mess up your query!

    The function search_post_excerpt is defined right down, at line 132, and you can see he adds excerpt to the search query.

    So you have 2 solutions:

    The bad one, is comment the line 114!

    //add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
    

    The good one, I didn’t implement myself, but should be something like add a filter on posts_where that runs after WooCommerce and fix it. Or maybe remove the filter.

    I’ll search better for the good solution, and I promisse I’ll post it here. But the bad solution do the job pretty well.

  2. So far your plugin looks good. Could you dump the final SQL string and add it to your question? Also: Does the WC plugin save everything in the posts table as post type? If so: Did you add a restriction to search only by this post type? You’ll probably need to do that in the $posts_clauses-filter in the $clauses['where'] clause or inside the posts_where-filter. Simply string replace the post_type = post part with your custom post type from WC.

Comments are closed.