Support

Account

Home Forums General Issues Advanced Post Object Query

Solving

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.

    Cheers,
    Klaye

  • 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

    
    <?php 
      
      // displaying apartment(s)
      while(have_posts()) {
        the_post();
        // 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()) {
            $resort_query->the_post();
            
            // display information about special
            
          } // end while 
          wp_reset_post_data();
        } // 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.