用 woocommerce 搜索替换主题搜索功能
Replace theme search functionality with woocommerce search
有没有一种方法可以用 woocommerce 产品搜索覆盖主题的搜索查询。我不想在页面和 post 中搜索内容,而是只在产品中搜索。
我搜索并找到了这个:
function custom_search_query($where, &$wp_query)
{
global $wpdb;
if ( empty( $where ))
return $where;
// get search expression
$terms = $wp_query->query_vars[ 's' ];
$where = 'wp_posts.post_type = "product"';
// get searcheable_acf, a list of advanced custom fields you want to search content in
$list_searcheable_acf = list_searcheable_acf();
foreach( $exploded as $tag ) :
$where .= "
AND (
(wp_posts.post_type LIKE '%$tag%')
OR (wp_posts.post_content LIKE '%$tag%')
OR EXISTS (
SELECT * FROM wp_postmeta
WHERE post_id = wp_posts.ID
AND (";
foreach ($list_searcheable_acf as $searcheable_acf) :
if ($searcheable_acf == $list_searcheable_acf[0]):
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
else :
$where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
endif;
endforeach;
$where .= ")
)
OR EXISTS (
SELECT * FROM wp_comments
WHERE comment_post_ID = wp_posts.ID
AND comment_content LIKE '%$tag%'
)
OR EXISTS (
SELECT * FROM wp_terms
INNER JOIN wp_term_taxonomy
ON wp_term_taxonomy.term_id = wp_terms.term_id
INNER JOIN wp_term_relationships
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE (
taxonomy = 'post_tag'
OR taxonomy = 'category'
OR taxonomy = 'myCustomTax'
)
AND object_id = wp_posts.ID
AND wp_terms.name LIKE '%$tag%'
)
)";
endforeach;
return $where;
}
add_filter( 'posts_search', 'custom_search_query', 500, 2 );
但这会在搜索中添加高级自定义字段。我只想搜索产品。
这可能吗?
提前致谢。
您应该使用 pre_get_posts 过滤器来过滤搜索结果页面中的 post 类型。例如:
add_filter( 'pre_get_posts', 'custom_pre_get_posts' );
function custom_pre_get_posts( $query ) {
if ( is_search() ) {
$query->set('post_type', 'product');
}
return $query;
}
这里唯一的问题是,如果您的产品有自定义字段或属性,它们将不会包含在结果中,因为默认的 WordPress 搜索只查找标题和内容。
您提供的示例具有更复杂的搜索,它也在自定义字段中查找。但如果主要目标是删除产品以外的所有内容,我的代码就可以解决问题。
下面的这段代码应该只搜索 "product" post 类型,并将显示在您的正常搜索存档中。通过在名称属性中使用 'post_type',您可以声明要搜索的 post 类型。
<form action="<?php bloginfo('siteurl'); ?>" method="get" class="form-inline searchform">
<input type="text" name="s" value="" placeholder="Search for Products..." class="s" />
<input type="hidden" name="post_type[]" value="product" />
<button type="submit" class="searchsubmit">Search</button>
</form>
有没有一种方法可以用 woocommerce 产品搜索覆盖主题的搜索查询。我不想在页面和 post 中搜索内容,而是只在产品中搜索。
我搜索并找到了这个:
function custom_search_query($where, &$wp_query)
{
global $wpdb;
if ( empty( $where ))
return $where;
// get search expression
$terms = $wp_query->query_vars[ 's' ];
$where = 'wp_posts.post_type = "product"';
// get searcheable_acf, a list of advanced custom fields you want to search content in
$list_searcheable_acf = list_searcheable_acf();
foreach( $exploded as $tag ) :
$where .= "
AND (
(wp_posts.post_type LIKE '%$tag%')
OR (wp_posts.post_content LIKE '%$tag%')
OR EXISTS (
SELECT * FROM wp_postmeta
WHERE post_id = wp_posts.ID
AND (";
foreach ($list_searcheable_acf as $searcheable_acf) :
if ($searcheable_acf == $list_searcheable_acf[0]):
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
else :
$where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
endif;
endforeach;
$where .= ")
)
OR EXISTS (
SELECT * FROM wp_comments
WHERE comment_post_ID = wp_posts.ID
AND comment_content LIKE '%$tag%'
)
OR EXISTS (
SELECT * FROM wp_terms
INNER JOIN wp_term_taxonomy
ON wp_term_taxonomy.term_id = wp_terms.term_id
INNER JOIN wp_term_relationships
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE (
taxonomy = 'post_tag'
OR taxonomy = 'category'
OR taxonomy = 'myCustomTax'
)
AND object_id = wp_posts.ID
AND wp_terms.name LIKE '%$tag%'
)
)";
endforeach;
return $where;
}
add_filter( 'posts_search', 'custom_search_query', 500, 2 );
但这会在搜索中添加高级自定义字段。我只想搜索产品。
这可能吗?
提前致谢。
您应该使用 pre_get_posts 过滤器来过滤搜索结果页面中的 post 类型。例如:
add_filter( 'pre_get_posts', 'custom_pre_get_posts' );
function custom_pre_get_posts( $query ) {
if ( is_search() ) {
$query->set('post_type', 'product');
}
return $query;
}
这里唯一的问题是,如果您的产品有自定义字段或属性,它们将不会包含在结果中,因为默认的 WordPress 搜索只查找标题和内容。 您提供的示例具有更复杂的搜索,它也在自定义字段中查找。但如果主要目标是删除产品以外的所有内容,我的代码就可以解决问题。
下面的这段代码应该只搜索 "product" post 类型,并将显示在您的正常搜索存档中。通过在名称属性中使用 'post_type',您可以声明要搜索的 post 类型。
<form action="<?php bloginfo('siteurl'); ?>" method="get" class="form-inline searchform">
<input type="text" name="s" value="" placeholder="Search for Products..." class="s" />
<input type="hidden" name="post_type[]" value="product" />
<button type="submit" class="searchsubmit">Search</button>
</form>