0

我正在尝试获取某些 post_types 的结果,但是,当我添加过滤器以检查 post_title 和 post_content 是否为“LIKE”查询字符串时,将返回所有 post_types,包括附件 post_types,我不希望显示在$args。这是我正在尝试的代码:

$query_string = get_search_query();
$args = array(                      
        'post_type' => array("hc_pillow", "hc_topper", "hc_accessory", "page"),                                                                                             
        'meta_query' => array(
            'relation' => 'OR',
            array(
                    'key' => 'tab_retail',
                'value' => $query_string,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'tab_tech',
                'value' => $query_string,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'tab_shipping_information',
                'value' => $query_string,
                'compare' => 'LIKE'
            )
        ),                      
        'posts_per_page' => "-1"                        
);
function filter_where_title($where = '') {
    $query_string = get_search_query();
    $where .= " OR post_title LIKE '%".$query_string."%' OR post_content LIKE '%".$query_string."%'";                   
    return $where;
}
$posts = new WP_Query();
add_filter('posts_where', 'filter_where_title');
$posts->query($args);                
remove_filter('posts_where', 'filter_where_title');

任何帮助都将不胜感激,我不确定还有什么可以尝试的。,

4

1 回答 1

0

您的 WHERE 语句的结构应如下所示:

WHERE `post_type` IN ( 'hc_pillow', 'hc_topper', 'hc_accessory', 'page' ) AND ( `post_title` LIKE '%search_string%' OR `post_content` LIKE '%search_string%' )

现在更像

WHERE `post_type` IN ( 'hc_pillow', 'hc_topper', 'hc_accessory', 'page' ) OR `post_title` LIKE '%search_string%' OR `post_content` LIKE '%search_string%'

这三个条件中的任何一个条件都足以使一行生效。

于 2012-07-20T21:39:49.437 回答