This will work:
$latest_post = get_post( array( 'cat' => YOUR_CAT_ID, 'posts_per_page' => 1) )
if( $latest_post ) {
echo get_permalink( $latest_post->ID );
}
Or you can do it in bulk for more categories:
$categories = array( 7, 12, 14, 15 );
foreach( $categories as $cat ) {
$latest_post = get_post( array( 'cat' => $cat, 'posts_per_page' => 1) )
if( $latest_post ) {
echo get_permalink( $latest_post->ID );
}
}
Related Posts:
- why same category base and single post base don’t work?
- Exclude or Include category ids in WP_Query
- Search custom taxonomy term by name
- WP_Query to show post from a category OR custom field
- Display recent posts from the same category as current post in sidebar
- How to filter by category in REST API, excluding posts also in other category term?
- Counting number of posts with Category B in Category A
- How to order category.php loop by ‘meta_value’?
- Use Transient API to cache queries for all posts in all categories?
- How can I display recent posts from a particular category in my header?
- Determine WP_Query parameters from URL
- How to show only one post for each categories of taxonomy of custom post that contains a specific custom field
- Get the post permalink within the loop but without additional DB query
- How get posts from a subcategory of a category by name?
- Get image of latest post from taxonomies/categories
- How To Remove/hide some specific categories from two different categories widget from sidebar
- WP_Query arguments to fetch custom post type posts which are in certain category?
- How do I reset this wp_list_categories query?
- Rewrite rule to prettify two $_GET variables while in a new endpoint from a page
- How to show subcategories using loop?
- List of the years with posts presented
- Remove a specific category ID from related post
- Complex Category selection as per user input
- Show all post for a given category
- Five posts from a category in footer
- WordPress add_rewrite_rule() cannot visit lower url levels
- How to get all unique categories for posts in loop?
- Display Featured Post by Categories and Avoid duplicated posts
- Cluster WooCommerce products in each Category Archive by Tags assigned to products
- Category applied to pages, creates multiple breadcrumb entries after a search query (On the translated site)
- Can we return all category (not post) with Custom Query Filter? [closed]
- Query posts intersecting tags and categories
- Query posts from category A, and from either category B or C
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- Combining categories (Query posts with multiple taxonomy terms)
- Check if loop has any categories?
- Filtering out child category posts from parent category archive not working
- How to display an other custom post type in a different custom post type’s archive?
- Exclude Category filter from Portfolio section
- Pagination for blog posts reloading same page [closed]
- Inserting HTML to close and open divs in WP_Query loops
- Finding WordPress Posts assigned to multiple categories
- Include posts from some categories while excluding from others
- Combine results of multiple WP_Query to resemble single WP_Query
- WordPress Custom Search Form Displaying Unexpected Results
- Default permalink structure causing Notice: Undefined property: WP_Query::$post
- wp_query is showing posts from other categories
- Hide products in uncategorized category from search results
- Loop posts based on permalink term
- How to display a list of posts in same child category as current post
- Slider won’t work with custom query
- WP_Query with one category in args shows other categories
- How to exclude a category name from showing?
- Loop categories by recent post
- How order posts from category by date and comment count?
- Add URL Rewrite Rule To WordPress
- How to get posts by category and by choosing a taxonomy term?
- What is wrong with my WP_Query Arguments?
- Custom Taxonomy Breaks Pages
- Widgets: Show Recent Posts Only if the Posts Have Both Categories X and Y
- get_the_terms has strange result since version 6.0
- Filter sub-category from checkbox form
- `offset` WP_Query argument dont work via `pre_get_posts`
- Use get_cat_ID to retreive multiple category IDs
- Function the_posts_pagination() not compatible with WP_Query arguments
- Filter products on category AND tag
- adding pagination to a foreach loop in wordpress
- Generate custom output on URL with directory
- 3 posts from each existing category on one page
- Get categories within specific term
- Related categories order posts by category
- How to show specify category template for both parent and child category
- Exclude parent categories from recent posts list
- Display All Top Child Categories / Taxonomy
- Query post by Category and custom file (ACF)
- Check the product in the cart from which category is and show message
- Filtering ‘Featured’ posts from a batch of category IDs
- Show posts from categories instead of tags
- Multiple values in WP_Query : category__and
- How to exclude posts by ID within a category/archive loop
- I have 3 categories, i want to display on a loop the last 3 of every category
- Adding Category in WP_Query Not Working
- display all posts from category with and without terms in chronological order
- Pull posts from all categories if quantity is not met?
- Retrieving category pages from subcategory returns empty sets
- query.php – multiple is_category functions
- Display Count of posts
- Create multiple sections for all categories and then queries all the posts for each of those categories
- Make assigning post to a specific category equivalent to assigning it to all categories
- query_posts problem – need help
- Filter posts by category
- List categories using WP_Query
- The permalink redirecting to current article on single.php page
- I need to get all categories from a WP_Query
- Advanced Search – Is this possible?
- How to get post taxonomy url and name in wp_query
- Pagination only showed when no category is set in wp_query
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2
- Excluding a category from frontpage but not from WP_Query
- How do I show related posts from categories instead of tags?