2

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:

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.

0

3 Answers 3

2

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.

0

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.

6
  • 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. Commented Sep 4, 2013 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. Commented Sep 4, 2013 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
    Commented Sep 4, 2013 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? Commented Sep 4, 2013 at 16:54
  • @kristinachilds Haven't you coded the snippet in the question?
    – kaiser
    Commented Sep 4, 2013 at 17:14
0

I am lost. Does it work now and what is the resulting code? I found a solution which works for Posts search but couldn't adapt it for products. How to limit wp-admin to search only titles

I don't want the Admin Search look into the small description. Just the Title would be enough.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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