Swa*_*226 5 wordpress woocommerce
我创建了一个名为 的元键_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');
Run Code Online (Sandbox Code Playgroud)
生成的查询:
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
Run Code Online (Sandbox Code Playgroud)
我如何用before替换AND关键字,以便我可以获得有条件的结果。OR( dsl_postmeta.meta_key = '_custom_product_catalog_number'OR
请尝试这个,
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;
}
Run Code Online (Sandbox Code Playgroud)
如需更多帮助,请参阅此链接:单击此处
| 归档时间: |
|
| 查看次数: |
2017 次 |
| 最近记录: |