如何搜索帖子的标题','内容' meta_query'没有WP_Query' s?

时间:2016-11-26 09:08:35

标签: php wordpress search

我在wordpress中编写了一个搜索查询,用于搜索' post_title',' post_content' &安培; ' meta_query&#39 ;.而不是使用WP_Query搜索'参数,我可以使用WP_Query' post' &安培; ' meta_query'相关参数与“OR”相结合以便搜索查询与' post_title'匹配。和' post_content'通过只传递WP_Query的相关参数? OR有没有办法用$ wpdb手动编写查询,如下所示:

global $wpdb;
$query= "SELECT wp_dxwe_posts.* FROM `wp_posts` WHERE 
(wp_posts.post_status IN ('$status_array') AND
wp_posts.post_type='messages') AND 
(wp_posts.post_title LIKE '%$keyword%' OR wp_posts.post_content 
LIKE '%$keyword%') AND ( wp_postmeta.meta_key == 'my_meta_key' AND 
wp_postmeta.meta_value LIKE '%$keyword%') ";

$my_query = $wpdb->get_results($query);

任何人都可以告诉我怎么样?提前谢谢。

我收到以下MySql结果查询:

  SELECT * FROM `wp_dxwe_posts` wp 
INNER JOIN `wp_dxwe_postmeta` wm ON (wm.`post_id` = wp.`ID`) 
WHERE wp.`post_status` IN ('publish,pending,draft') 
AND wp.`post_type`='messages'
AND (wp.`post_title` LIKE '%prasad%' OR 
wp.`post_content` LIKE '%prasad%') 
    AND wm.`meta_key` = 'inistitute_name' 
    AND wm.`meta_value` LIKE '%some_institute_name%'

1 个答案:

答案 0 :(得分:0)

以下列方式使用自定义查询:

SELECT * FROM `wp_posts` wp 
    INNER JOIN `wp_postmeta` wm ON (wm.`post_id` = wp.`ID`) 
        WHERE wp.`post_status` IN ('$status_array') 
        AND wp.`post_type`='messages'
        AND (wp.`post_title` LIKE '%$keyword%' OR 
wp.`post_content` LIKE '%$keyword%') 
        AND wm.`meta_key` = 'my_meta_key' 
        AND wm.`meta_value` LIKE '%$keyword%'