admin管理员组文章数量:1122846
is it possibile to search custom post type product
by _sku
or other custom meta field with input text field?
I don't want to use plugin, just need custom WP function.
I have found this solution for search only by title:
function __search_by_title_only($search, &$wp_query)
{
global $wpdb;
if (empty($search))
return $search; // skip processing - no search term in query
$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);
... but I need function like this for meta fields ( _sku
and _author
).
Thanks !
is it possibile to search custom post type product
by _sku
or other custom meta field with input text field?
I don't want to use plugin, just need custom WP function.
I have found this solution for search only by title:
function __search_by_title_only($search, &$wp_query)
{
global $wpdb;
if (empty($search))
return $search; // skip processing - no search term in query
$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);
... but I need function like this for meta fields ( _sku
and _author
).
Thanks !
Share Improve this question edited Apr 19, 2023 at 11:48 Mahesh Thorat 1054 bronze badges asked Feb 1, 2018 at 11:05 Mladen JankovicMladen Jankovic 112 bronze badges 1- You search needs seem relatively complex. for simplicity and performance I'd really reconsider not using a plugin. SearchWP, for example, would work well for your needs as I understand them. – Jacob Peattie Commented Feb 1, 2018 at 12:27
1 Answer
Reset to default 0This might work, not tested though. First add this to join the postmeta table:
add_filter( 'posts_join', 'search_filters_join', 501, 2 );
function search_filters_join( $join, $query ) {
global $wpdb;
if ( empty( $query->query_vars[ 'post_type' ] ) || $query->query_vars[ 'post_type' ] !== 'product' ) {
return $join; // skip processing
}
if ( ($this->is_ajax_search() || $this->is_search_page()) && !is_admin() ) {
$join .= " INNER JOIN $wpdb->postmeta AS customsearch ON ( $wpdb->posts.ID = customsearch.post_id )";
}
return $join;
}
Then modify your $search
-variable in the foreach-loop as to include the following (this example shows _sku
, just add on _author
to another OR
section if you want to search for that as well):
$search .= "{$searchand} (($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR (customsearch.meta_key='_sku' AND customsearch.meta_value LIKE '{$n}{$term}{$n}'))";
本文标签: phpSearch custom post type posts only by meta fields
版权声明:本文标题:php - Search custom post type posts only by meta fields? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736291843a1928809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论