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.

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


<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" />

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.


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.

| improve this answer | |

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.

| improve this answer | |
  • That's what if($_GET['post_type'] = 'product' ) is supposed to be... I don't see post_type = post anywhere. Am I missing something here? The post type for WooCommerce products is product. I'm not sure what you mean by final SQL string. – kristina childs Sep 4 '13 at 5:11
  • Perhaps I should clarify. It IS searching just the product post type, however it is searching the main content as well as titles, which results in less-than-ideal results when someone is searching for a specific product. I want it to search title field only. – kristina childs Sep 4 '13 at 5:15
  • @kristinachilds I already left some steps that are needed in the answer, like showing us the complete resulting SQL string. Please do so. Then we can talk further. So far it's guessing on my side. – kaiser Sep 4 '13 at 13:32
  • I don't know what you mean by resulting SQL string. I just put in the php and the database spits stuff out :) Can you give me an example? – kristina childs Sep 4 '13 at 16:54
  • @kristinachilds Haven't you coded the snippet in the question? – kaiser Sep 4 '13 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.