Skip to main content
11 votes

Unable to prevent function using save_post firing twice

The approved answer didn't work for me. I ended up trying several conditionals and the email would still get sent twice: function xxx_send_mail($id, $post, $update){ if (defined('...
Uriahs Victor's user avatar
7 votes
Accepted

$update is always true in save_post

So appreciate this is a bit late but I was having the exact same issue, the $update parameter is almost completely useless if you want to check whether it is a new post or not. The way I got around ...
Alex Baulch's user avatar
6 votes

Add validation and error handling when saving custom fields?

When save_post runs, it has already saved the post on the database. Looking into WordPress core code, more specifically at the wp-includes/post.php's update_post() function, there is no built-in way ...
Lucas Bustamante's user avatar
5 votes

Using save_post to replace the post's title

Here's a solution that uses a static variable to prevent an infinite loop. This allows you to safely call wp_update_post() inside of a function that is hooked to save_post. function ...
Kellen Mace's user avatar
5 votes
Accepted

Removing Uncategorized on post save if other category present?

Yes. You can use the save_post action and do it here is some function that remove the default wordpress category if there is some other category selected. I added some comments so you will understand ...
Shibi's user avatar
  • 1,843
5 votes
Accepted

Unable to prevent function using save_post firing twice

First, you can use this hook to target only one custom type: https://developer.wordpress.org/reference/hooks/save_post_post-post_type/ This hook (and save_post) is called the first time when you ...
mmm's user avatar
  • 3,713
5 votes

Unable to prevent function using save_post firing twice

Already approved answers have not worked for me and comment from @Nico Pernice helped. If conditions in my code look like these: function reset_cpt_expire( $post_id, $post, $update ){ // Check if ...
Saqib's user avatar
  • 53
4 votes
Accepted

Exclude trash from save_post

save_post gets fired, once the post is saved. We get the current post object as the second parameter. So we can check, if the current post status is trash: <?php add_action( 'save_post', function( ...
websupporter's user avatar
  • 2,989
4 votes

Is there an easy way to AJAX-ify saving of post?

Brian Fegter's response was the right idea, but it had a few bugs. Here's a more polished solution based on the same principle. Step 1: PHP Logic. Place in functions.php (or a plugin file) // ...
Jess Mann's user avatar
  • 108
4 votes
Accepted

flush_rewrite_rules on save_post Does Not Work on First Post Save

I know this has already been answered, but I felt as if it wasn't 100% real clear what the actual solution was. Here's my answer to add some clarification. He's right... You can't flush rewrite ...
Michael Ecklund's user avatar
4 votes
Accepted

WP Rest API - Upload media without saving attachment post

Based on your comment you appear to be using the REST API. There's no hook between uploading the file and creating the attachment post that could be used for this purpose in the API endpoint. The ...
Jacob Peattie's user avatar
4 votes
Accepted

WordPress function saves a post twice and updates all posts

It duplicate because when you run the wp_update_post(), it will use the wp_insert_post() function and the action save_post will run again. please use the filter wp_insert_post_data to filter the ...
Hồ Trọng Linh Ân's user avatar
4 votes
Accepted

Custom Post Meta from custom metaboxes is randomly being deleted

The code uses the save_post hook to save the results of the form, however, at no point did it check which post it was saving. This means your save function runs for that post, but it also runs for ...
Tom J Nowell's user avatar
  • 59.9k
3 votes
Accepted

Check if value has changed on save_post

In the end I checked against and updated a new meta value on each save. $screen = get_current_screen(); if ( $screen->base == 'post' && $screen->post_type == 'sessions') { if ( ...
Picard102's user avatar
  • 933
3 votes

Why save_post_$(custom_post_type) is fired even if I am not already saving a post?

You should not have a nonce in your metabox, and obviously not check for it in your save handler. I know everybody copied the codex page on creating metaboxes but the use of nonce there is just ...
Mark Kaplun's user avatar
  • 23.4k
3 votes

Unable to save or update postmeta for custom post type via action using save_post hook

The save_post hook will fire whenever WordPress saves a post to the database. This includes saving WP revisions which will have a different post ID than the actual post. It's more than likely you are ...
Nathan Johnson's user avatar
3 votes

Why not fire the save_post event?

The save_post action hook fires after a post is saved: every time, even for autosaves. If you look at the reference for save_post, you'll see that the callback receives three parameters: the first is ...
Nathan Johnson's user avatar
3 votes
Accepted

Get updated meta data after save_post hook

Apparently in order to get the meta value right after saving the post, you have to either get the $_REQUEST['my_meta_value'] directly, or, if you need to update the database right away, you have to ...
Eckstein's user avatar
  • 1,099
3 votes

How can you receive the most recent permalink or terms of the newly saved post?

When using the Gutenberg Block editor you need to use different hooks to get the expected behavior. rest_insert_{$this->post_type} The pre-update hook. rest_after_insert_{$this->post_type} The ...
Mark's user avatar
  • 1,029
3 votes
Accepted

Save values from a pre_post_update

The second attempt is closer to the result, as it is filer, not action, and you can return proper data. But code has several mistakes, I have fixed them. The proper number of parameters for the filter ...
KAGG Design's user avatar
  • 1,842
3 votes
Accepted

Change post author without using wp_update_post()

On the few occasions when I've needed to do something similar, I've used the callback unattach-update-reattach method, which is also described on this older Q&A, Update post on save and on the ...
Antti Koskinen's user avatar
3 votes
Accepted

Run function after a post has finished saving - callback function perhaps?

If you don't have control over the API, you're doing it almost right. That is what cron jobs are for. What needs to be avoided is possible overlapping API calls that do the same thing. Why not instead ...
maryisdead's user avatar
2 votes

Check for update vs new post on save_post action

here is a functional code that worked and tested by me i used in my website also it solve the two following problem associated with save_post action : problem to check between ubdate or insert ...
Nejmeddine Jammeli's user avatar
2 votes

Check for update vs new post on save_post action

I have just encountered the save_post about new and update. After reading the source code to understand the flow. I found that the following method might be useful. Since it is not yet mentioned ...
西門 正 Code Guy - JingCodeGuy's user avatar
2 votes

Non-hierarchical custom taxonomy using checkboxes on edit-screen -> saving issue

This seems to be a core bug, spotted at 4.5 as well. Although documentation implies otherwise, you can not select in practice terms of non hierarchical taxonomies with the "checkoxed" metabox, the ...
Mark Kaplun's user avatar
  • 23.4k
2 votes

Hook to process a new taxonomy tag before it is created?

The hook you're looking for is created_{$taxonomy} but there's a another issue. I think the biggest flaw in the logic is putting both states and cities in the same taxonomy. You'll never be able to ...
Howdy_McGee's user avatar
  • 20.7k
2 votes
Accepted

Add post meta based on another post meta value before publish post

The solution is using added_post_meta and updated_post_meta hook . Here is the working code . add_action( 'added_post_meta', 'add_custom_field_automatically', 10, 4 ); add_action( '...
Mithun Sarker Shuvro's user avatar
2 votes

Reset all transients on post or page save

The technical side of it depends on the storage used. For default database storage the transient entries can be queried and deleted, since they have specific naming format. For enabled Object Cache ...
Rarst's user avatar
  • 99.7k
2 votes
Accepted

Update post on save

A post's title (post_title) is not saved in meta data; it's a field within the post table. Here's an updated version of your original code. Infinite loop is prevented by removing and then readding ...
Dave Romsey's user avatar
  • 17.8k
2 votes

Manipulating post meta in the customizer

Please see the Customize Posts plugin. It has a WP_Customize_Post_Setting and WP_Customize_Postmeta_Setting for representing posts and postmeta in the customizer, respectively.
Weston Ruter's user avatar
  • 4,040

Only top scored, non community-wiki answers of a minimum length are eligible