Php WP_查询变量不随循环重置

Php WP_查询变量不随循环重置,php,wordpress,Php,Wordpress,下面是一个场景: 我有三个问题 第一个查询按当前用户返回产品 $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的元键的帖子 $custom_p

下面是一个场景: 我有三个问题 第一个查询按当前用户返回产品

$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的元键的帖子

$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);
}
现在,第三个查询针对相同的自定义帖子类型,但它只返回与第一个查询匹配的帖子。 这里有一个代码可以让它更清晰

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。
我需要代码方面的帮助,以便修复此问题

您好,我认为这是因为您使用 'p'=>$matching\u id WP_查询中的键“p”需要一个具体的post_id,使用此参数的查询始终只返回一个WP_post。 代码$matching_ID中还有另一个错误。在这个代码中,始终只包含一个ID,但是这个变量应该包含一个post_ID数组

if ($rel_post == $current_user_product) {
                $matching_id = $custom_post->ID;
}
您应该使用“post\u in”而不是“p”,并且$matching\u id必须是一个数组

$third_query = new WP_Query( array( 'post_type' => ['custom_post'], 'post__in' => $matching_ids ) );

欢迎Wick先生:)您的第三个代码块显示了对
$matching\u id
的赋值,
$third\u query
使用
$matching\u id
这是一个打字错误吗?您能告诉我如何将$matching\u id转换为数组吗?我不知道,我假设您的代码中应该有一个循环,在该循环中收集一些id到数组中,如果($rel_post==$current_user_product){$matching_id[]=$custom_post->id;}循环结束$matching_id[]-一些帖子的数组已工作!!!谢谢