您的问题和您提供的代码之间存在一些矛盾,因此我不确定您正在处理的两个场景中的哪一个。
场景1-要显示具有特定值的所有店铺shop-id
以及所有具有相同特定价值的餐厅restaurant-id
. 假设值为20。你可以用一个WP_Query()
循环,用于查找自定义字段具有指定值的两种帖子类型。这假设商店永远不会对restaurant-id
反之亦然。
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => array( \'shop\', \'restaurant\' ),
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'restaurant-id\',
\'value\' => 20,
\'compare\' => \'=\'
),
array(
\'key\' => \'shop-id\',
\'value\' => 20,
\'compare\' => \'=\'
)
)
);
$query = new WP_Query( $args );
场景2-您希望显示所有店铺,每个店铺后面都是具有相同价值的所有餐厅
restaurant-id
作为当前店铺的
shop-id
.
$shops = new WP_Query(
array(
\'posts_per_page\' => -1,
\'post_type\' => \'shop\'
)
);
if ( $shops->have_posts() ) {
while ( $shops->have_posts() ) {
$shops->the_post();
...
$shop_id = get_post_meta( $post->ID, \'shop-id\', true );
$restaurants = new WP_Query(
array(
\'posts_per_page\' => -1,
\'post_type\' => \'restaurant\',
\'meta_query\' => array(
array(
\'key\' => \'restaurant-id\',
\'value\' => $shop_id,
\'compare\' => \'=\'
)
)
)
);
if ( $restaurants->have_posts() ) {
while ( $restaurants->have_posts() ) {
$restaurants->the_post();
...
}
}
}
}