Support

Account

Home Forums General Issues Ordering a Relationship Field

Solved

Ordering a Relationship Field

  • Hi,

    I’m trying to return a relationship field that has some ‘active testimonials’ in it, but in a random order. I can use a ‘get_posts’ array to define the order as being random, but this bypasses the relationship field altogether and shows a randomised list of ALL the testimonials (not just the ones in the relationship field).

    Can anyone please suggest how I would go about limiting the array to just the items in the relationship field?

    http://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/

    $posts = get_posts(array(
                'post_type'     => 'testimonial',
                'posts_per_page'  => -1,
                'orderby'     => 'rand'
              ));

    Many thanks,
    Andy.

  • Set your relationship field to return an array of post IDs and then Use post__in.

    
    $related = get_field('relationship_field');
    $posts = get_posts(array(
        'post_type'     => 'testimonial',
        'posts_per_page'  => -1,
        'orderby'     => 'rand',
        'post__in' => $related
    ));
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Ordering a Relationship Field’ is closed to new replies.