我创建了一个名称为_custom_product_catalog_number
的元密钥,并且希望向人们提供可以使用此目录号搜索产品的功能。
我在functions.php中添加了以下代码
add_action('pre_get_posts', 'modify_post_search');
function modify_post_search($query)
{
if($query->is_search)
{
$s="c2500";//$s=$_GET['s'];
$query->set('meta_query', [
[
'key' => '_custom_product_catalog_number',
'value' => $s,
'compare' => '='
]
]);
}
}
add_filter('pre_get_posts', 'filter_search');
生成的查询:
SELECT SQL_CALC_FOUND_ROWS dsl_posts.ID
FROM dsl_posts
INNER JOIN dsl_postmeta ON ( dsl_posts.ID = dsl_postmeta.post_id )
WHERE 1=1
AND ( dsl_posts.ID NOT IN (
SELECT object_id FROM dsl_term_relationships WHERE term_taxonomy_id IN (6)
) )
AND (((dsl_posts.post_title LIKE '%c2500%') OR (dsl_posts.post_excerpt LIKE '%c2500%') OR (dsl_posts.post_content LIKE '%c2500%')))
AND ( /* replace AND with OR */
( dsl_postmeta.meta_key = '_custom_product_catalog_number' AND dsl_postmeta.meta_value = 'c2500' ) )
AND dsl_posts.post_type = 'product' AND (dsl_posts.post_status = 'publish' OR dsl_posts.post_status = 'wc-want-be-returned' OR dsl_posts.post_status = 'private')
GROUP BY dsl_posts.ID
ORDER BY dsl_posts.post_title LIKE '%c2500%' DESC, dsl_posts.post_date DESC LIMIT 0, 12
我如何在AND
之前用OR
替换( dsl_postmeta.meta_key = '_custom_product_catalog_number'
关键字,以便获得具有OR
条件的结果。
答案 0 :(得分:0)
请尝试这个,
add_action('pre_get_posts', 'modify_post_search');
function modify_post_search($query)
{
$query->set( 'meta_query', array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => '_my_custom_key',
'value' => 'Value I am looking for',
'compare' => '='
),
array(
'relation' => 'AND',
array(
'key' => '_my_custom_key_2',
'value' => 'Value I am looking for 2',
'compare' => '='
),
array(
'key' => '_my_custom_key_3',
'value' => 'Value I am looking for 3',
'compare' => '='
)
)
);
return $query;
}
有关更多帮助,请参见以下链接:Click Here