{"id":5562055,"date":"2014-12-10T00:56:09","date_gmt":"2014-12-10T00:56:09","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/related-article-script-not-working-100\/"},"modified":"2016-08-22T13:22:36","modified_gmt":"2016-08-22T13:22:36","slug":"related-article-script-not-working-100","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/related-article-script-not-working-100\/","title":{"rendered":"Related article script not working 100%"},"content":{"rendered":"<p>Hi there.<\/p>\n<p>I tried to modify a related article script from tags to categories. Now it seemed to work except that it kind of generates random suggestions that are not even from the same category.<\/p>\n<pre><code>&lt;div class=&quot;relatedposts&quot;&gt;\n\t\t\t\t\t  &lt;h2&gt;Related posts&lt;\/h2&gt;\n\t\t\t\t\t&lt;?php\n\t\t\t\t\t\t$orig_post = $post;\n\t\t\t\t\t\tglobal $post;\n\t\t\t\t\t\t$categories = get_the_category($post-&gt;ID);\n\n\t\t\t\t\t\tif ($categories) {\n\t\t\t\t\t\t\t$category_ids = array();\n\t\t\t\t\t\tforeach($categories as $individual_category) $category_ids[] = $individual_category-&gt;term_id;\n\t\t\t\t\t\t\t$args=array(\n\t\t\t\t\t\t\t\t&#039;category__in&#039; =&gt; $category_ids,\n\t\t\t\t\t\t\t\t&#039;post__not_in&#039; =&gt; array($post-&gt;ID),\n\t\t\t\t\t\t\t\t&#039;posts_per_page&#039;=&gt;3, \/\/ Number of related posts to display.\n\t\t\t\t\t\t\t\t&#039;caller_get_posts&#039;=&gt;1,\n\t\t\t\t\t\t\t\t&#039;orderby&#039;=&gt;&#039;rand&#039; \/\/Random\n\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t$my_query = new wp_query( $args );\n\n\t\t\t\t\t\twhile( $my_query-&gt;have_posts() ) {\n\t\t\t\t\t\t\t$my_query-&gt;the_post();\n\t\t\t\t\t\t?&gt;\n\n\t\t\t\t\t\t&lt;div class=&quot;relatedthumb&quot;&gt;\n\t\t\t\t\t\t\t&lt;a rel=&quot;external&quot; href=&quot;&lt;? the_permalink()?&gt;&quot;&gt;&lt;?php the_post_thumbnail(array(287,208)); ?&gt;&lt;br \/&gt;\n\t\t\t\t\t\t\t&lt;?php the_title(); ?&gt;\n\t\t\t\t\t\t\t&lt;\/a&gt;\n\t\t\t\t\t\t&lt;\/div&gt;\n\n\t\t\t\t\t\t&lt;?php }\n\t\t\t\t\t\t}\n\t\t\t\t\t\t$post = $orig_post;\n\t\t\t\t\t\twp_reset_query();\n\t\t\t\t\t\t?&gt;\n\t\t\t\t\t&lt;\/div&gt;<\/code><\/pre>\n<p>Any suggestions where I went wrong?<\/p>\n<p>regards<\/p>\n","protected":false},"template":"","class_list":["post-5562055","topic","type-topic","status-closed","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/5562055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/5562055\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=5562055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}