Support

Account

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();
    	}
    }