Home Forums General Issues Query Relationship Fields by setup_postdata method


Query Relationship Fields by setup_postdata method

  • Situation
    I have a custom post type named Packs and another custom post type named Hotels. In the Hotels CPT I publish hotels information, like name, amenities and services for many hotels in the world. Moreover, in the Pack CPT I have many advanced custom fields for publishing a travel pack, like prices, departures, stays, and a relationship field where I’m able to select the appropriated hotels previously created for this pack. In the page single-pack everything should be displayed, like a travel voucher – including the hotels.

    I’d like to query the posts of this relationship field. If I selected 10 hotels to display in a specific pack, I’d like to randomize them and manipulate the posts per page. I used a method like get_post but I used get_field instead:

      $att1s = get_field('pack_hotel');
        'posts_per_page' => 1,
        'orderby'        => 'rand'
      if( $att1s ):
      foreach( $att1s as $post->$args):
    <b>Hotel name: <?php the_field('hotel_name'); ?></b>
    <b>Hotel services: <?php the_field('hotel_services'); ?></b>

    I had no joy with that. How can I make the get_field(‘pack_hotel’); recognize that I need only one post per page and randomize it!

    Thank you!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.