Making relatd posts
-
I’m modifying a function that gets posts related by cat and tag to the current post. I’m having trouble after modifying it’s still not showing what I want.
Original
public static function get_related_posts($max_posts = 20){ $my_posts = array(); $post_id = get_the_ID(); $tags_string = ''; $post_tags = get_the_tags(); if ($post_tags) { foreach ($post_tags as $post_tag) { $tags_string .= $post_tag->slug . ','; } } $query = array( 'exclude' => $post_id, 'numberposts' => $max_posts, 'tag' => $tags_string ); $get_relateds = apply_filters('essgrid_get_related_posts', $query, $post_id); $tag_related_posts = get_posts($get_relateds); if(count($tag_related_posts) < $max_posts){ $ignore = array(); foreach($tag_related_posts as $tag_related_post){ $ignore[] = $tag_related_post->ID; } $article_categories = get_the_category($post_id); $category_string = ''; foreach($article_categories as $category) { $category_string .= $category->cat_ID . ','; } $max = $max_posts - count($tag_related_posts); $excl = implode(',', $ignore); $query = array( 'exclude' => $excl, 'numberposts' => $max, 'category' => $category_string ); $get_relateds = apply_filters('essgrid_get_related_posts_query', $query, $post_id); $cat_related_posts = get_posts($get_relateds); $tag_related_posts = $tag_related_posts + $cat_related_posts; } foreach($tag_related_posts as $post){ $the_post = array(); if(method_exists($post, "to_array")) $the_post = $post->to_array(); else $the_post = (array)$post; if($the_post['ID'] == $post_id) continue; $my_posts[] = $the_post; } return apply_filters('essgrid_get_related_posts', $my_posts); }
stripping out all tag related code
public static function get_related_posts($max = 20){ $my_posts = array(); $post_id = get_the_ID(); $article_categories = get_the_category($post_id); $category_string = ''; foreach($article_categories as $category) { $category_string .= $category->cat_ID . ','; } $query = array( 'exclude' => $post_id, 'numberposts' => $max, 'category' => $category_string ); $get_relateds = apply_filters('essgrid_get_related_posts_query', $query, $post_id); $cat_related_posts = get_posts($get_relateds); foreach($cat_related_posts as $post){ $the_post = array(); if(method_exists($post, "to_array")) $the_post = $post->to_array(); else $the_post = (array)$post; if($the_post['ID'] == $post_id) continue; $my_posts[] = $the_post; } return apply_filters('essgrid_get_related_posts', $my_posts); }
But I’m getting everything returned. I’m not a dev. I don’t even know how to start debuggin. Do you see what’s wrong?
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Making relatd posts’ is closed to new replies.