如何在WordPress中修改WP_QUERY

发布于 2025-01-21 12:15:54 字数 852 浏览 3 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2025-01-28 12:15:54

不幸的是,您无法在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文