下面是一个场景:我有3个查询,第一个查询返回当前用户的产品
$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);
}
现在,第三个查询针对相同的custom_post类型,但它只返回与第一个查询匹配的帖子。这里有一个代码,可以让它变得更清晰
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。我需要代码方面的帮助,这样我才能解决这个问题
发布于 2021-04-09 15:45:42
你好,我想这是因为你在WP_Query中使用了'p‘=> $matching_ids键'p’期望一个具体的post_id,而使用这个参数的查询总是只返回一个wp_post。在代码$matching_ids中还有另一个错误,因为这段代码总是只包含一个ID,但是这个变量应该包含一个post_ids数组
if ($rel_post == $current_user_product) {
$matching_id = $custom_post->ID;
}
应使用“post__in”而不是“p”,并且$matching_ids必须是数组
$third_query = new WP_Query( array( 'post_type' => ['custom_post'], 'post__in' => $matching_ids ) );
https://stackoverflow.com/questions/67016748
复制相似问题