Home › Forums › ACF PRO › Auto Fill Related Post objects › Reply To: Auto Fill Related Post objects
Untested but here’s something to get you started:
$post_objects = get_field( 'testimonials_related' );
if ( $post_objects ) {
// your normal stuff here..
} else {
// no post objects found, query the post type for 4 random posts
// you should cache this query with either the object cache or transients api
// http://wordpress.stackexchange.com/questions/183698/way-to-cache-a-query-for-24-hrs
$current_post_id = get_the_id();
$testimonials = new WP_Query(
'post_type' => 'testimonials',
'post_status' => 'publish',
'posts_per_page' => 4,
'orderby' => 'rand',
'post__not_in' => array( $current_post_id ) // exclude the current post
);
if ( $testimonials->have_posts() ) {
while ( $testimonials->have_posts() ) {
$testimonials->the_post();
// display the posts...
}
wp_reset_postdata();
}
}
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.