9
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('...
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 ...
6
votes
save_post action only when creating a new post
The save_post action also passes three parameters to your callback, one of which being $update which denotes whether the post being saved is an existing post or not.
/**
* Save post metadata when a ...
6
votes
Execute action after post is saved with all related post_meta records (data)
Quite stupid solution, but works:
function afterPostUpdated($meta_id, $post_id, $meta_key='', $meta_value=''){
if($meta_key=='_edit_lock') {
if($_GET['message']==1) {
//
...
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 ...
5
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 ...
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 ...
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 ...
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)
// ...
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 ...
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( ...
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 ...
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 ...
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 ...
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 ( ...
3
votes
Check for update vs new post on save_post action
Example to ialocin answer with "update" paremeter:
function save_func($ID, $post,$update) {
if($update == false) {
// do something if its first time publish
} else {
// Do something ...
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 ...
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 ...
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 ...
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 ...
3
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 ...
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 ...
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 ...
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 ...
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
...
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 ...
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 ...
2
votes
Allow only one post with specific meta value
I want this condition to only be applied to one post, i.e. when the
user declares a post as "featured", and saves it, all the other ones
(theoretically only the last one) that have this meta value ...
2
votes
Accepted
Using PODS data with save_post
Yes, whenever a post is saved you should see all of the input items in $_POST ... if you're not using something like xdebug to debug with, you could do something like:
echo "<pre>";
var_dump( $...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
save-post × 384post-meta × 57
custom-post-types × 56
metabox × 54
custom-field × 40
hooks × 37
actions × 37
plugin-development × 25
functions × 25
php × 22
posts × 22
custom-taxonomy × 18
advanced-custom-fields × 17
wp-insert-post × 17
plugins × 13
wp-update-post × 12
customization × 11
categories × 10
filters × 10
publish × 10
terms × 9
autosave × 9
block-editor × 8
database × 7
quick-edit × 7