admin管理员组文章数量:1123504
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.
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.
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Sep 4, 2013 at 0:51 kristina childskristina childs 4341 gold badge7 silver badges22 bronze badges 04 Answers
Reset to default 2This 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.
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.
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.
This probably stopped working in version 3.6.0 (2019).
If you've gone crazy looking for a solution (as I have) because the information out there is older than this date
Here is an update of the answer
Since Woocommerce 3.6.0 there is a function called “search_products” in the file includes/data-stores/class-wc-product-data-store-cpt.php
And exists one hook to override woocommerce_product_pre_search_products
.
Only need change one line
add_filter( 'woocommerce_product_pre_search_products', 'override_woo_search_products', 10, 6 );
function override_woo_search_products($custom_query=false, $term, $type, $include_variations, $all_statuses, $limit) {
...
$term_group_query .= $wpdb->prepare( " {$searchand} ( ( posts.post_title LIKE %s) OR ( wc_product_meta_lookup.sku LIKE %s ) $variation_query)", $like, $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
...
}
本文标签: custom post typesWooCommerce product search titles only
版权声明:本文标题:custom post types - WooCommerce product search titles only 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736576699a1944871.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论