Support

Account

Home Forums General Issues Query Relationship Fields by setup_postdata method

Unread

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.

    Problem
    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:

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

    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)

The topic ‘Query Relationship Fields by setup_postdata method’ is closed to new replies.