Home Forums General Issues Advanced Post Object Query


Advanced Post Object Query

  • Hey, I have 3 custom post types – Resorts, Apartments, Specials.

    ‘Apartments’ have a ‘Resort’ post object field – to load resort data onto each relevant apartment page.

    ‘Specials’ have a ‘Resort’ post object field – To link to each resort where a special is available.

    Is there any way I can query ‘Specials’ from within ‘Apartments’ to display a list of specials on each apartment page?

    I can’t add ‘Specials’ within ‘Apartments’ as a post object because there are far too many apartments for the client to manage this effectively.

    Any ideas on how I can accomplish this would be much appreciated.


  • Never mind, we’ve decided to select apartments from within the specials post type.

  • For anyone looking for something similar in the future, this is one way to do this

      // displaying apartment(s)
      while(have_posts()) {
        // display other information about the apartment
        // get post ID of resort from apartment page
        // assumes post object field does not allow multiple
        $resort = get_field('resort', false, false);
        // query specials post type for same resort
        // again, assumes post object field does not allow multiple
        $args = array(
          'post_type' => 'special',
          'posts_per_page' => -1,
          'meta_query' => array(
            'key' => 'resort',
            'value' => $resort
        $resort_query = WP_Query($args);
        if ($resort_query->have_posts()) {
          while($resort->have_posts()) {
            // display information about special
          } // end while 
        } // end if have_posts
        // possibly show more info about apartment after special query
      } // while have posts
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Advanced Post Object Query’ is closed to new replies.