Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9219207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:03:37+00:00 2026-06-18T03:03:37+00:00

I am trying show related posts through posts linked through a category in wordpress

  • 0

I am trying show related posts through posts linked through a category in wordpress but I am having trouble filtering the results.

Here is my code at the moment:

     $current_post = $post->ID;
         $i = 0;
         $categories = get_the_category();
         foreach ($categories as $category) {            
            $posts = get_posts('numberposts=4&category='. $category->term_id . '&exclude=' . $post->ID);
                foreach($posts as $post) { 
                                     // DO BASIC ECHO POST CONTENT STUFF

                     $i++;
                     if ($i == 3) break;
                         }
           } 


wp_reset_query();

The problem with my code is that when a post is in 3 categories (even if that isn’t good web practise) this loop echo’s 12 posts (4 posts per category) and if a different article is in the same 3 categories, it is shown 3 times (duplicated). I want to show MAX 4 posts, and have no duplicates.

I thought the $i == 3 break; would make it stop after the first ‘global’ 4 results. But it doesn’t? and how can I make it so there are no duplicate results in the results?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:03:38+00:00Added an answer on June 18, 2026 at 3:03 am
    $current_post = $post->ID;
             $i = 0;
             $categories = get_the_category();
             foreach ($categories as $category) {            
                $posts = get_posts('numberposts=4&category='. $category->term_id . '&exclude=' . $post->ID);
                    foreach($posts as $post) { 
                                         // DO BASIC ECHO POST CONTENT STUFF
    
                         $i++;
                         if ($i == 3) break 2;
                             }
               } 
    

    wp_reset_query();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in modifying a template in a wordpress install to show related posts
I know there have been several posts already about Sudoku-related questions, but I am
I'm trying to show data from two related entities in a DataGridView. I know
I am trying to make a function to show 5 related keywords for whatever
So I have a query that is trying to grab related posts. Categories have
I'm trying to develop a WP plugin to show post(or page) related information (such
I am trying to get the related products block to show up on my
I am trying to show five product-related thumbnails just below the product image (to
Trying to show a label only when a certain item in a combo is
I'm trying to show/hide( or simply put - filter ) my markers( of houses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.