0 ){ $rel_count = 0; $rel_posts = array(); $tag_ids = array(); $get_page_id = get_the_ID(); if ( has_tag() ) { $tags = wp_get_post_tags($get_page_id); foreach($tags as $tag): array_push( $tag_ids, $tag -> term_id); endforeach ; $tag_args = array( 'post__not_in' => array($get_page_id), 'posts_per_page'=> $max_post_num, 'tag__in' => $tag_ids, 'orderby' => 'rand', 'post_type' => 'page', ); $rel_posts = get_posts($tag_args); $rel_count = count($rel_posts); } if(!has_tag() || $max_post_num > $rel_count){ $categories = get_the_category($get_page_id); if(!empty($categories)){ $category_ID = array(); foreach($categories as $category): array_push( $category_ID, $category -> cat_ID); endforeach ; $cat_args = array( 'post__not_in' => array($get_page_id), 'tag__not_in' => $tag_ids, 'posts_per_page'=> ($max_post_num - $rel_count), 'category__in' => $category_ID, 'orderby' => 'rand', 'post_type' => 'page', ); $cat_posts = get_posts($cat_args); $rel_posts = array_merge($rel_posts, $cat_posts); shuffle($rel_posts); } } if( count( $rel_posts ) > 0 ): ?>