here is a function that does just that:
function get_lastest_post_of_category($cat){
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
$post_is = get_posts( $args );
return $post_is[0]->ID;
}
Usage: say my category id is 22 then:
$last_post_ID = get_lastest_post_of_category(22);
you can also pass an array of categories to this function.
Related Posts:
- Stop WordPress automatically adding tags to post content
- Set Default Listing “View” in Admin
- How to add category to: ‘wp-admin/post-new.php’?
- How to prevent posts from being published too close to each other?
- Let private posts stay in status “private” after edit through “editors”
- how to get a different html for odd/even posts?
- How to publish a post with empty title and empty content?
- Change the Slug of Post Type post to baseurl/post/%postname%
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- How to display posts by current user/author in a custom page template?
- featuring old articles without messing up with the archive
- display sub categories assoccited with each post in a category archive page
- Include related posts on a page
- White screen of death on post save only
- Embed WordPress Admin in an iframe
- Can’t publish, edit or delete posts since Gutenberg update
- ping_status in JSON REST API
- How do I batch create revisions of all posts?
- How can i do something after head like adding a hook for after head but before post
- How to get the post’s parent ID?
- How to get only text from wordpress post content?
- Save re-arranged draggable post items to wordpress database
- I want to get title name of post, but i didn’t get by this code. Any suggestion?
- How to modify post content?
- Allow public to post on blog
- Facebook doesn’t display the related thumbail for some posts
- Suddenly my last posts disappeared
- Emojis replaced by ‘?’ automatically
- WordPress Multisite – Create Default Post and New Category On New Site Install
- Creating a post from data returned from HTML form
- Query Posts by Custom Field
- Get Post’s first image using Short-code
- Delete post results in “Cannot delete or update a parent row: a foreign key constraint fails”
- What are the benefits of setting a “featured image” for a WP blog post?
- Format latest/newest post differently
- Loop doesn’t exclude the specified category in home page
- is_page Funtion for Posts ?
- show posts under subcategory only when in that subcategory
- Modify posts listing at back end
- How to update all posts at once?
- How to allow multiple thumbnail upload for Posts?
- Loop through posts? Want to remove extra newlines in actual post HTML/content on backend
- How include css class based on post ( in loop ) slug?
- WordPress Loop: How to display recent posts in multiple divs
- Random posts that always include a particular post?
- When moving a WordPress installation to a new server, why do I have to update each post before content shows?
- Show number of posts by logged in user
- How does WP decide how to display a page
- Cannot implement byline into posts container in single.php
- Post title is not showing
- Change Author Name to Sitename on Frontend
- Insert data from custom created PHP page into wp_postmeta table
- Inserting Media to WordPress Posts
- Problem with trying to display subheader in posts
- Custom Post Type, post-formats associated to custom post type > 404 archives
- Add Text Domain To Post Navigation
- Internal linking to posts permalink fail because of spaces and stripe at postname
- Change content off every sixth element
- Custom post type and taxonomy page templates
- What would causes search to return incorrect results?
- How to implement a WordPress comments function?
- WordPress – When visiting a `single post/ single.php` my category menu item link becomes active
- How can I restore posts from ‘trash’ with their previous post_status? – WordPress
- Pagination not give another posts
- Shortcode for Comments and Comment Boxes in Post Loop
- Get post slug and match with menu item slug to change css
- My page shows some other content in WP
- Weird problem occurs while using WordPress REST API to create new post
- Can’t find webpage on All Pages or All Post menus
- Centre Blog Index (Posts Page) on Home.php
- Dropdown Select Post Filter
- Display Specific Categories posts on the home page
- Add postmeta to all blog posts
- Enable HTML Tags in WordPress Post excerpt
- Code to display random ordered posts in Categories across pages [duplicate]
- Modify loop to include all post statuses not just ‘published’
- How to convert return type of ‘get_posts()’ same as ‘wp_get_recent_posts()’?
- How to give priority to page than to posts permalink structure?
- Embedding Facebook in blog post not working
- Add category information beneath post?
- Post List Widget with custom posts and editing the Read More Link
- how to remove entry meta from wordpress archive category and change its display
- Save/retrieve form data best practises for theme development
- Removing standard links in the posts page
- How to handle a post before publishing?
- Changing permalink of custom category
- Special Query: Title, Terms, Content – %LIKE%
- Multiple post back-to-back display only one gallery
- Error establishing a database connection
- How to limit the number of posts a user can publish based on user type
- Auto selection of category based on subject line or TAG
- Single post pagination
- posts_per_page – Repeats only first posts ‘post__in’ array
- Add News Feed to bespoke website, only to one page on whole site
- Display post count on archive page in reverse order
- How can add custom commands in post-new.php in wordpress
- Filter/Sort Post Form On Taxonomy page
- Restore posts, but only posts that exist in my database backup
- How to move all images in a post to a single gallery for a bunch of posts?
- Display post by select it from a dropdown menu