EduardET

Untitled

Jun 29th, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.67 KB | None | 0 0
  1. function wpc_extra_get_post_related_posts() {
  2.     $post_id = get_the_ID();
  3.     $terms = get_the_terms($post->ID, array('category', 'post_tag'));
  4.  
  5.     $term_ids = array();
  6.     if ( is_array( $terms ) ) {
  7.         foreach ( $terms as $term ) {
  8.             $term_ids[] = $term->term_id;
  9.         }
  10.     }
  11.  
  12.     $related_posts = new WP_Query( array(
  13.         'tax_query'      => array(
  14.             array(
  15.                 'taxonomy' => 'category',
  16.                 'field'    => 'id',
  17.                 'terms'    => $term_ids,
  18.                 'operator' => 'IN',
  19.             ),
  20.         ),
  21.         'post_type'      => 'post',
  22.         'posts_per_page' => '4',
  23.         'orderby'        => 'rand',
  24.         'post__not_in'   => array( $post_id ),
  25.     ) );
  26.  
  27.     if ( $related_posts->have_posts() ) {
  28.         return $related_posts;
  29.     } else {
  30.         return false;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment