Ok you can use a foreach loop to return the latest post from each category.
Here is how you should do that.
<?php
$postids = array();
$catids = array( 1, 2, 3 ); // add category ids here.
foreach ( $catids as $catid ) {
$my_query = new WP_Query( array( 'cat' => $catid, 'ignore_sticky_posts' => 1, 'posts_per_page' => 1, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false ) );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
$postids[] = $post->ID;
endwhile;
endif;
wp_reset_postdata();
}
print_r( $postids ); // printing the array.
?>
It’s practically the same as running the 3 different loops but code is much cleaner now.
Related Posts:
- Categories Listing and Highlighting current category item
- Exclude a ‘portfolio’ custom category?
- Get Posts Under Custom Taxonomy
- How to add category to: ‘wp-admin/post-new.php’?
- Use category base slug in posts’ permalink
- Setting Custom Sort Order of Posts within a Category
- Different number of posts in each category
- Display the latest post from a category in a page
- How to show list of posts by author and category?
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- How to control which category will be picked for the slug of a post?
- get_the_term_list by hierarchy order
- How to change the layout and styling of posts according to their category in WordPress?
- Add default content to post (for specific category)
- Query only Posts from Both of Two Category?
- how to limit and display tag?
- display sub categories assoccited with each post in a category archive page
- Display most recent post in category instead of archive?
- How can I specify a category post on my home page
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- get_post_type_archive_link(‘post’) returns current category archive
- Permalink Settings: optional settings doesn’t apply to editor
- Get user’s most published categories
- Conditional that checks if a current category has any subcategory with posts
- How to set post expiration date and time and move the page to archive after expiration [closed]
- Exclude posts from featuring
- I want to show category in the post title , how can I do
- Articles are text-only in any “Category” instead of HTML
- How To Remove Certain category Posts From the Blog Page
- How can i sort the categories by ID
- Add icon/badge next to posttitle of specific category
- Add ajax load more function using WP API
- Get current category id from post page wordpress
- Saving category to a post, before publishing the post
- Exclude category from
- Assign category to new post via URL
- Stop users posting into category while still seeing contents of the category
- Category Page Template not Showing Posts
- How to Restrict Previous & Next Post Link to Posts of Same Category?
- WordPress custom Query for Posts in Category display posts multiple times
- Categories list into registration form
- WordPress Multisite – Create Default Post and New Category On New Site Install
- Using ajax on editing a category edit page?
- How to display two random-post sections that are each under their own category
- Loop doesn’t exclude the specified category in home page
- How to create a sub post?
- Post Image not displaying in category view
- WordPress bulk category select when publishing post
- Exclude some categories from listing on the current post
- how can i display my posts alphabetically?
- Change Post Title For Specific Category
- In what context should Categories and Tags be used
- list posts from two categories on a page – plugin not working
- Are post, page and category IDs unique to each other?
- Static pages not working
- WP_Query of Category Not Showing First Post
- 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?
- 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
- Save / Update meta data as multidimensional array
- 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?
- WordPress not showing recent posts
- How to filter out post from a category not its subcategory in wordpress dashboard
- Add category information beneath post?
- How can I display a specific number of post in a category via a url
- how to chang preview of list of articles of one category?
- Newly created category does not show category
- Post of a specific category – not to be seen in paging previous/next post
- How to show main category related to that post
- How to pass multiple checkbox values to wordpress query?
- Sort post by category using HTML Select tag
- Make assigning post to a specific category equivalent to assigning it to all categories
- Show a Category Specific Info Box
- Display the first post’s comments of category in comments.php template
- How to filter categories of a post
- ‘get_previous_post’ in same category returning (!empty) when no previous post in category exists
- 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
- Only show posts belonging to multiple cateogries at same time
- Wrap posts from a category in HTML
- posts_per_page – Repeats only first posts ‘post__in’ array
- How to display my categories in a list (to a post, not the sidebar)?
- How to create a cumulative posts and members count
- How to make category for word post_content