Support

Account

Home Forums General Issues Get a random item from a relationship field Reply To: Get a random item from a relationship field

  • And if I try to reduce the array to one element only, I get an error and, at the bottom I don’t get the right related post but another one, outside the original related group.


    $posts = get_field('related_content');
    $random_post = array();
    array_push($random_post,$posts[array_rand($posts)]);
    if( $random_post ):
    setup_postdata($random_post);
    the_title();
    endif;

    returns a bunch of:

    Notice: Trying to get property of non-object in /Users/totanus/Sites/ovestsesia/wordpress/wp-includes/query.php on line...

    And then prints an incorrect the_title();

    I’m missing something, of course…

    Teo