我已经为我的自定义帖子编写了一个搜索查询。我正在为此使用WP\\u Query的搜索参数,然后它必须搜索“post\\u title”和“post\\u内容”,但它只搜索“post\\u title”,而不搜索“post\\u内容”。下面是我的代码:
$institute_name = get_user_meta($user_ID, \'inistitute_name\', true);
//add_filter( \'posts_where\' , array($this,\'posts_where\' )); this line Irritated me.
$paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
$args = array(
\'post_type\' => \'mypost\',
array( \'s\' => $keyword ),
\'meta_query\' => array (
array (
\'key\' => \'inistitute_name\',
\'value\' => array ($institute_name),
\'compare\' => \'IN\'
)
),
\'posts_per_page\' => -1,
\'paged\' => $paged,
\'post_status\' => \'publish\',
);
$the_query = new WP_Query($args);
//remove_filter( \'posts_where\', array($this,\'posts_where\' ));
echo "<pre>";
print_r($the_query);
echo "</pre>";
如果我打印该结果,那么我将得到如下查询:
SELECT wp_dxwe_posts.* FROM wp_dxwe_posts INNER JOIN wp_dxwe_postmeta ON
( wp_dxwe_posts.ID = wp_dxwe_postmeta.post_id ) WHERE 1=1 AND
( (wp_dxwe_postmeta.meta_key = \'inistitute_name\' AND
CAST(wp_dxwe_postmeta.meta_value AS CHAR) IN (\'ITeLearn\') ))
AND wp_dxwe_posts.post_type = \'mypost\' AND ((wp_dxwe_posts.post_status = \'publish\')) AND
post_title LIKE \'%launch%\' GROUP BY wp_dxwe_posts.ID
ORDER BY wp_dxwe_posts.post_date DESC
正如我所说,它只适用于“post\\u title”,但有时不起作用,它并不总是起作用。谁能告诉我这个代码有什么问题吗?提前谢谢。
更新:下面是我在同一个文件中找到的“post\\u where”方法。我认为最好删除此方法,或者不要通过“add\\u filter”方法调用“posts\\u where”方法。最后,我通过注释“add\\u filter”得到了解决方案&;\'删除\\u filter的代码行。但你能简单地告诉我一下“add\\u filter”方法吗?我在评论“add\\u filter”方法时会遇到任何问题吗。?我正在更新上述代码。
function posts_where( $where ) {
if(isset($_GET[\'tewa_search\'])) {
$var=$_GET[\'tewa_search\'];
$where .= " AND post_title LIKE \'%".$var."%\'";
}
return $where;
}