ID); $args = []; // set args to get related posts based on tags if (!empty($tags)) { $tag_ids = []; foreach($tags as $tag) $tag_ids[] = $tag->term_id; $args=[ 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'ignore_sticky_posts' => true, 'posts_per_page'=>5, 'orderby'=>'rand', ]; } else { // get all cats assigned to current post $cats = get_the_category($post->ID); // set the args to get all related posts based on category. if ($cats) { $cat_ids = []; foreach($cats as $cat) $cat_ids[] = $cat->term_id; $args=[ 'category__in' => $cat_ids, 'post__not_in' => array($post->ID), 'ignore_sticky_posts' => true, 'posts_per_page'=>5, 'orderby'=>'rand', ]; } } if(!empty($args)){ // build the markup and return $rp_loop = new WP_Query($args); $aps_rp = '