我正在使用wordpress,我想传递一个参数name "post_title"在wp_query,所以我可以改变查询,可以得到正确的数据,但我的查询没有改变(不显示如我预期的),我尝试了以下代码
$args = array(
'post_type' => 'user',
'posts_per_page' => -1,
'post_title' => $_GET['post_title'],
'post_status' => [ 'publish']
);
$query = new WP_Query($args);
当我打印$query->请求时,我得到以下查询
[request] = "SELECT MZAGsQVeposts.* FROM MZAGsQVeposts WHERE 1=1 AND MZAGsQVeposts.post_type = 'user' AND ((MZAGsQVeposts.post_status = 'publish')) ORDER BY MZAGsQVeposts.post_date DESC";
但是期望查询是
"SELECT MZAGsQVeposts.* FROM MZAGsQVeposts WHERE 1=1 AND MZAGsQVeposts.post_type = 'user' AND ((MZAGsQVeposts.post_status = 'publish')) AND MZAGsQVeposts.post_title='Lisa I' ORDER BY MZAGsQVeposts.post_date DESC";
我错在哪里?提前谢谢。
不幸的是,您不能在wp_query中按标题查询帖子,这不是一个公认的参数。(参见WP_Query获取更多信息)然而,你可以使用get_page_by_title并以这种方式传递标题,它将返回带有该标题的post对象。