Ejemplo n.º 1
0
 public static function get_related_post($args = array())
 {
     $related_posts = array();
     $post_id = '';
     $posts_per_page = 5;
     $post_type = 'post';
     extract($args, EXTR_OVERWRITE);
     if (empty($post_id) && (is_single() || is_page() || is_singular())) {
         $post_id = get_the_ID();
     }
     $tags = SB_Post::get_tag_ids($post_id);
     $defaults = array('post_type' => $post_type, 'tag__in' => $tags, 'posts_per_page' => -1);
     $defaults = wp_parse_args($defaults, $args);
     $posts = self::get($defaults);
     $tag_posts = $posts->posts;
     $cats = SB_Post::get_category_ids($post_id);
     $defaults = array('post_type' => $post_type, 'category__in' => $cats, 'posts_per_page' => -1);
     $defaults = wp_parse_args($defaults, $args);
     $posts = self::get($defaults);
     $cat_posts = $posts->posts;
     $a_part = SB_PHP::get_part_of(2 / 3, $posts_per_page);
     foreach ($tag_posts as $post) {
         if ($post->ID == $post_id || in_array($post, $related_posts)) {
             continue;
         }
         array_push($related_posts, $post);
     }
     $related_posts = array_slice($related_posts, 0, $a_part);
     if (count($related_posts) < $a_part) {
         $a_part_new = $posts_per_page - count($related_posts);
     } else {
         $a_part_new = $posts_per_page - $a_part;
     }
     $count = 0;
     foreach ($cat_posts as $post) {
         if ($post->ID == $post_id || in_array($post, $related_posts)) {
             continue;
         }
         array_push($related_posts, $post);
         $count++;
         if ($count >= $a_part_new) {
             break;
         }
     }
     return $related_posts;
 }