3

I have two post types:

  1. Venues
  2. Reviews

The Venues Post Type contains the following ACF custom fields:

  • Region
  • Sub Region

The Reviews Post Type contains one ACF custom field:

  • Venue (which is Post Object - Select Field)

I need to display all Reviews who's Venue is in a specific region and/or Sub Region.

Is this something that can be accomplished using WP_Query? Or do I need to do a fancy database call?

This is what I thought would initially work but it seems that you can not get the custom field of a post object as a meta_query..

$args = array(
  'post_type' => 'review',
  'posts_per_page' => 18,
  'paged' => $paged,
  'meta_key' => 'venue',
  'meta_query' => array(
     array(
       'key' => 'region',
       'value' => 'napa-valley'
     )
   )
);


John Garcia
  • 58
  • 1
  • 10

1 Answers1

7

I think you need 2 loops here, first loop through the venues using region meta query (you could just use get_posts() or get_pages() instead of WP_Query too) e.g.

'meta_query' => array(
    array(
        'key' => 'region',
        'value' => 'napa-valley'
        )
)

Then you can push the IDs of the venues in specific regions into an array

array_push($venue_ids, $post->ID);

Then you can use the $venue_ids array in your second loop which would loop through the review using a meta query to match the venues from your first loop ids to the post object ids selected in the review page.

'meta_query' => array(
    array(
        'key' => 'venue',
        'value' => $venue_ids
        )
)

Let me know if this is helpful and if you think this will work for you and I can offer further assistance if I haven't explained correctly or you need help.

Paul
  • 1,412
  • 1
  • 9
  • 19
  • Thanks Paul, this mostly makes sense. The only issue I have is that the meta_query for 'venue' is not working. I would assume this is because the 'venue' is an ACF post object that returns an object, instead of just an ID. Is there any way to access the id of the post object and use it as the key? (hopefully that makes sense) – John Garcia Jun 11 '19 at 17:36
  • I figured it out. Post Objects are stored as a serialized array. If i changed, "array_push($venue_ids, $post->ID);" to "array_push($venue_ids, serialize(array((string)$post->ID)));" it works perfectly. Thanks for the help! – John Garcia Jun 11 '19 at 18:28
  • Also in ACF sustom field settings when you set up your post object you can select if an object or id is returned, that's what my example was based on and it cleans up your code as little as there's no need for serialize. – Paul Jun 12 '19 at 08:14