I believe you should look into status transitions: https://developer.wordpress.org/reference/hooks/transition_post_status/
There are a couple of examples below the explanation on top of the page. Here’s also some example code of mine:
add_action( 'transition_post_status', 'nxt_create_news', 10, 3 );
// Automatically create a news when a wiki post has been published
function nxt_create_news($new_status, $old_status, $nxt_wiki_post) {
if('publish' === $new_status && 'publish' !== $old_status && $nxt_wiki_post->post_type === 'wiki') {
... do something here
}
In your case, the old_status should be ‘publish’ and the new status as well. Make sure to find a way to prevent an endless loop, but I believe that the examples on the WP documentary should prove useful. 🙂
Related Posts:
- What to use: wp_is_post_autosave( $post_id ) or DOING_AUTOSAVE?
- Better post meta efficiency?
- How can I pass $post object to ‘save_post’ add_action?
- Have save_post write to database image meta [closed]
- Get the post_id of a new post
- publish_post conflicts with save_post
- How to get post bulk edit action trigger and get edited post ids?
- Get Meta Key Value While Saving Post
- Updating post data on save (save_post vs wp_insert_post_data)
- save_post hook is not called when post is saved
- Change post author without using wp_update_post()
- Update post meta within save_post action
- Save / Update meta data as multidimensional array
- Get Category in save_post Hook
- problem with admin panel
- How to remove a column from the Posts page
- Hook that get’s triggered when the author of a post is changed
- Allow user to “edit_others_posts” to save only, not publish
- Find out who deleted a page or post?
- Skip post in loop and mixin later
- How to put last edited entry on top of the blog-post list?
- My ‘post attributes’ is missing from my WordPress install
- How do I sort (order) the results from `get_children()`?
- Reverse chronology of post listing
- Archived Posts in Wrong Category
- Get post embedded image caption
- Odd/even class on article tag
- WordPress get posts by date without query_posts
- WP_Query only ONE post, won’t work
- How to get posts by multiple post slugs? [duplicate]
- Changing next and previous post link text
- Disabled wp-cron, set up system cron but still have missed schedule posts
- Inserting custom data to the_post() during loop
- How To Hide A Post
- Get Image tag from content of post
- Visting slug for ‘post’ post type shows 404 not archive?
- Remove Custom Post Type Slug and add Custom Taxonomy to Permalink Structure?
- Automatically create a new page daily with last three posts
- Add filter to the end of the post
- Indirect modification of overloaded property WP_Post::$ancestors has no effect
- How do I change the location of post-specific information?
- ACF to select posts not displaying on blog page
- Benefits to using semantic HTML in post content? [closed]
- How can I insert a new markup element after 4 posts automatically? [closed]
- the_excerpt is displaying excerpts of every post
- Using wp_tag_cloud with only posts published in specific year
- Post: how to set created date after post has been published [closed]
- WordPress loop not working on static front page
- Sidebar on single.php not showing up
- remove all custom post created
- Hide content from Post Preview
- How to get the next and previous image title from attachment or gallery?
- Pagination for posts inside a specific category
- Creating a redirect option for current page url
- How to edit a custom theme?
- WordPress posts have a different URL to the index
- How to put the author of the post in the comments?
- Blog posts pagination not working
- unable to write to the database while uploading images
- Restricting displayed posts to posts from only select authors
- Set featured image as hero image on each page
- Get origin or context of a how a post got created and/or updated
- The letter “i” appears capitalised even when I don’t mean to capitalise them
- I really need help… I am looking for a solution about 28hours
- Custom Post Type Rewrite URL
- I want to show recent post according to category of the post in single.php?
- Posts content is display added with title, instead of content, using the_excerpt()
- WP_Query sort by meta_value_num or date
- Category and Posts in front page
- How do I get a certain set of posts from the database?
- Stop images from being displayed on the front-end of the site
- Add something after all sticky posts in The Loop
- How to show different categories in different pages?
- Query posts from a specific category and selected tag
- hypher.js doesn’t work to IE and Chrome
- Order by ‘s’ using WP_Query()
- Password protection doesn’t prompt for password on pages
- What effect can a large wp_post table have on overall site performance?
- Changes done in admin panel are taking time to reflect in the REST api responses
- How to make ‘show_option_all’, that comes from wp_list_categoreis, to work with get_categories or with get_terms?
- How to add a remove function?
- How do I save JS classes being applied to the WP dashboard via custom plugin?
- Automatically search and replace link in content (pages/posts)?
- How to update wp_posts with just the returned comment count from wp_comments (SQL GROUP BY)
- Is there a way to change the default post template selection for a specific category?
- my website didn’t show the defult right sidebars
- Admin notice on wp_insert_post
- Get related posts matching most of the provided tags using WP_Query
- I want to add category slug in posts url
- How to make internal links creating plugin to respect ACF?
- How to change url of posts?
- How to link Post Title to uploaded file?
- I have a blog and want to prompt the user to select a category they like and have that select a random blog post
- How can I customize sections of the homepage layout?
- How to remove category and other tags from posts page
- How to remove slug from CPT correctly?
- Download button under all images WordPress
- Get the category of post from which category page it have been clicked
- Adding Blocks within Blocks
- posts blocks don’t show up correctly