Home › Forums › Front-end Issues › Wp_query with relationship to another custom post type › Reply To: Wp_query with relationship to another custom post type
Ahhh, I see.. I assumed the other way around because it’s a 1 to many from hospitals to branches…
I can’t think of the logic then.. hmmm…
The goal is to find “recommended” branches right? And a branch is considered “recommended” if a “recommended” hospital is attached to it? Am I right so far?
If so… and you are connecting them in the way that you are… choosing 1 hospital for each branch… then….
1) Create an array of all hospital IDs that *are* recommended. Hint: You can use WP_Query to return *just* the IDs.
Here’s the info from this link:
/// Return Fields Parameter
Set return values.
fields (string) – Which fields to return. All fields are returned by default. There are two other options:
‘ids’ – Return an array of post IDs.
‘id=>parent’ – Return an array of stdClass objects with ID and post_parent properties.
Passing anything else will return all fields (default) – an array of post objects ///
2) Then… Query branches that contain those hospital IDs… you can do a meta query such as:
'meta_query' => array( array( 'key' => 'hospital', 'value' => array( 3, 4 ), // array of hospitals IDs from 1st query 'compare' => 'IN', ),
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.