There’s some contradiction between your question and the code you provided, so I’m not exactly sure which of two scenarios you’re dealing with.
Scenario 1 – You want to show all Shops with a specific value for shop-id
and all Restaurants with the same specific value for restaurant-id
. Let’s say that value is 20. You can do this with a single WP_Query()
loop that looks for both post types where either custom field has the specified value. This assumes that Shops will never have a value for restaurant-id
and vice-versa.
$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 );
Scenario 2 – You want to show all Shops, each Shop followed by all Restaurants that have the same value for restaurant-id
as the value of the current Shop’s 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();
...
}
}
}
}