首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WP_Query变量不随循环重置

WP_Query变量不随循环重置
EN

Stack Overflow用户
提问于 2021-04-09 15:23:39
回答 1查看 28关注 0票数 0

下面是一个场景:我有3个查询,第一个查询返回当前用户的产品

代码语言:javascript
运行
复制
$current_user_products = new WP_Query( array( 'post_type' => 'product', 'author' => get_current_user_id() ) );
foreach($custom_user_products->posts as $custom_user_product) {
}

现在,第二个查询返回具有产品id的元键的帖子

代码语言:javascript
运行
复制
$custom_posts = new WP_Query( array( 'post_type' => ['custom_post'] ) );
foreach($custom_posts->posts as $custom_post) {
            $rel_post = get_post_meta($custom_post->ID, 'ProductID', true);
}

现在,第三个查询针对相同的custom_post类型,但它只返回与第一个查询匹配的帖子。这里有一个代码,可以让它变得更清晰

代码语言:javascript
运行
复制
if ($rel_post == $current_user_product) {
                $matching_id = $custom_post->ID;
}
$third_query = new WP_Query( array( 'post_type' => ['custom_post'], 'p' => $matching_ids ) );
enter code here

我面临的问题是,第三个查询只返回1个post。我需要代码方面的帮助,这样我才能解决这个问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-09 15:45:42

你好,我想这是因为你在WP_Query中使用了'p‘=> $matching_ids键'p’期望一个具体的post_id,而使用这个参数的查询总是只返回一个wp_post。在代码$matching_ids中还有另一个错误,因为这段代码总是只包含一个ID,但是这个变量应该包含一个post_ids数组

代码语言:javascript
运行
复制
if ($rel_post == $current_user_product) {
                $matching_id = $custom_post->ID;
}

应使用“post__in”而不是“p”,并且$matching_ids必须是数组

代码语言:javascript
运行
复制
$third_query = new WP_Query( array( 'post_type' => ['custom_post'], 'post__in' => $matching_ids ) );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67016748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档