Support

Account

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….

    I would:

    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',
      ),
    

    Cool?