getfluid
3/9/2018 - 7:24 PM

Choose posts manually with ACF and fall back to random post if one isn't selected, excludes posts selected and the post that your currently

Choose posts manually with ACF and fall back to random post if one isn't selected, excludes posts selected and the post that your currently viewing.

@define $posts = get_field('related_projects')
@define $exclude = [get_the_id()]
@define $custom_posts = count($posts)

@if( $posts )
  @foreach($posts as $post)
     {{ setup_postdata($post) }}

    @define $exclude[] = $post->ID

    @include('templates.related-projects')

  @endforeach
  {{ wp_reset_postdata() }}
@endif

@unless($custom_posts > 2 )
  {{
    $args = [
      'post_type'      => 'projects',
      'orderby'        => 'rand',
      'post__not_in'   => $exclude,
      'posts_per_page' => (3 - $custom_posts)
    ];
  }}
  @wpquery($args)

    @include('templates.related-projects')

  @wpempty
  @wpend
@endunless