如何在WordPress中修改WP_QUERY
我正在使用WordPress,我想在WP_QUERY中传递一个参数名称“ Post_title”,因此我可以更改查询并可以获取正确的数据,但是我的查询没有更改(不像我预期的那样显示),我尝试了以下代码。
$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";
我错的地方吗?提前致谢。
i am working with wordpress,And i want to pass one parameter name "post_title" in wp_query,So i can change query and can get correct data,But my query is not changed(not displaying as i expected),I tried with following code
$args = array(
'post_type' => 'user',
'posts_per_page' => -1,
'post_title' => $_GET['post_title'],
'post_status' => [ 'publish']
);
$query = new WP_Query($args);
when i print my $query->request then i getting following 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";
But expected query is
"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";
Where i am wrong ? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,您无法在WP_QUERY中按标题查询帖子,这不是一个公认的论点。 (请参阅 wp_query 以获取更多信息) ://developer.wordpress.org/reference/functions/get_page_by_title/“ rel =“ nofollow noreferrer”> get_page_by_title 并以该标题返回帖子对象。
Unfortunately you can't query posts by title in wp_query, it's not a recognised argument. (See WP_Query for more info) however you can use get_page_by_title and pass the title that way and it'll return the post object with that title.