jmccole83
1/16/2020 - 4:03 PM

Wordpress | Query Multiple Category Terms

@php

  $post_cats = get_the_category( get_the_ID() );

  $cat_ids = array();

  foreach($post_cats as $cat){
    array_push($cat_ids, $cat->term_id);
  }

  $args = array(
    'post_type'         => 'post',
    'posts_per_page'    => 3,
    'post_status'       => 'publish',
    'order'             => 'DESC',
    'category__in'      => $cat_ids
  );

  $the_query = new \WP_Query( $args );

@endphp

@if($the_query->have_posts())
<div class="related-content my-5">
  <h3 class="text-center pb-5 text-teal">Related content</h3>
  <div class="d-flex flex-wrap justify-content-between">
    @while($the_query->have_posts()) @php $the_query->the_post() @endphp
    <div class="position-relative related-post d-flex justify-content-center text-white bg-grey mb-3" @if(has_post_thumbnail()) style="background-image: url({!! the_post_thumbnail_url() !!});" @endif>
      <h4 class="text-center"><a href="{!! get_permalink() !!}" class="stretched-link">{!! the_title() !!}</a></h4>
    </div>
    @endwhile
  </div>
</div>
@endif