One possibility:
$related = get_posts(
array(
'category__in' => wp_get_post_categories( $post->ID ),
'numberposts' => 5,
'post__not_in' => array( $post->ID )
)
);
if( $related ) {
foreach( $related as $post ) {
setup_postdata($post);
/*whatever you want to output*/
}
wp_reset_postdata();
}
Reference:
Answer re-written based on WP_Query()
:
$related = new WP_Query(
array(
'category__in' => wp_get_post_categories( $post->ID ),
'posts_per_page' => 5,
'post__not_in' => array( $post->ID )
)
);
if( $related->have_posts() ) {
while( $related->have_posts() ) {
$related->the_post();
/*whatever you want to output*/
}
wp_reset_postdata();
}
Related Posts:
- Get Posts Under Custom Taxonomy
- Different number of posts in each category
- Show related posts by category but ignore one category
- Getting Position of a post in a category
- Display only posts from a specific parent category
- get_the_term_list by hierarchy order
- How to change the layout and styling of posts according to their category in WordPress?
- display sub categories assoccited with each post in a category archive page
- How can I specify a category post on my home page
- Use template of parent category for single post sub-categories
- How to search only by post title and category?
- Can I hide a specific post from latest posts page?
- Permalink Settings: optional settings doesn’t apply to editor
- How to show post(excerpt) from specific category on wp page?
- Divide Loop Into Days & Categories
- Different template for posts of all subcategories of category
- I want to show category in the post title , how can I do
- Mark menu item as current-menu-item for category
- Random post category URL
- How can i sort the categories by ID
- Display posts in three columns by category
- Add ajax load more function using WP API
- Deleted category by mistake, all posts now have categorized category. Can i revert that?
- How to display category from recent posts?
- Can I divide the pages into categories?
- Properly display posts on homepage
- Display post X of Y in category
- Exclude category from
- Post page to display specific category
- WordPress Multisite – Create Default Post and New Category On New Site Install
- how to show single post in a custom template
- Using ajax on editing a category edit page?
- Disable sticky option for specific categories
- date issue with category post retrival
- How to make multiple sections in home pulling posts category wise?
- Excluding posts not working
- How to get subcategories from category slug?
- Remove current category from post but display all others
- Showing categories and subcategories with posts
- Change default category when I publish a post
- Loop doesn’t exclude the specified category in home page
- Post Image not displaying in category view
- How do I show posts from another wordpress installation?
- How do I include the category next to the title of a post?
- How to show a custom taxonomy in the theme?
- How to import nested categories from XML file?
- Migrate posts from category and sub-category via SQL
- Parent category / child category posts
- Posts Missing in Dashboard after update
- How to get post with slug and exclude categories
- Unable to differentiate between two categories under custom post type on single.php
- Exclude post category in a blog page
- Retrieving posts by their date and category
- Are post, page and category IDs unique to each other?
- WP_Query of Category Not Showing First Post
- WordPress displays post on subcategory only
- How to make posts being uncategorized
- How to display a single(current) post from a category
- displaying the categories post
- What’s a theme that properly handles previews of different types of posts?
- Categories Listing and Highlighting current category item
- How to add content above footer in posts from specific category
- WordPress – When visiting a `single post/ single.php` my category menu item link becomes active
- Author post count in category
- How to Create another Page Category like the “Post” and “Pages”
- How to create a button to filter posts (list) by meta value
- Is there a way to save different data when USER interacts with the same POST?
- How to display particular categorie’s post which associated to specific user?
- Display Specific Categories posts on the home page
- show only one category and filter by tag
- Automatic Table of contents with categories and posts
- Code to display random ordered posts in Categories across pages [duplicate]
- AJAX load more posts not using correct category and repeating the same few posts
- Category posts show on local install, they do not show on live server
- How can I exclude only a specific sub category post from category display?
- how to print total number of posts filtered by category?
- Is it possible to remove actual post pages in wordpress but keep them in categories
- Need help with hiding an image within a post in a category
- How to get post count including nested categories
- How to filter out post from a category not its subcategory in wordpress dashboard
- Add category information beneath post?
- Newly created category does not show category
- How to pass multiple checkbox values to wordpress query?
- Show a Category Specific Info Box
- About number of posts in selected category
- Display the first post’s comments of category in comments.php template
- How to filter categories of a post
- Category page shows “No Posts” but I do have two assigned
- Exclude a ‘portfolio’ custom category?
- List posts related to category on a div [closed]
- how to show all post with its contents
- wordpress show category link instead of post link [closed]
- CSS – Change height of Storefront / WooCommerce product categories, but not products
- excerpt in template for specific page
- Only show posts belonging to multiple cateogries at same time
- Wrap posts from a category in HTML
- How to display my categories in a list (to a post, not the sidebar)?
- How to make category for word post_content
- View post with specific category id and name which I selected in the backend (drop-down option)
- Filters do not work when there are multiple (one works)