Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created October 25, 2017 08:39
Show Gist options
  • Save ihorduchenko/c6764f8a45d2dd4ed1fbe3d28bed735f to your computer and use it in GitHub Desktop.
Save ihorduchenko/c6764f8a45d2dd4ed1fbe3d28bed735f to your computer and use it in GitHub Desktop.
WP_Query of related posts by array of categories of current post
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'posts_per_page' => '3',
'category__in' => $cats_ids,
'post__not_in' => array($post->ID)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment