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
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.