save_post is an action triggered whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. The data for the post is stored in $_POST, $_GET or the global $post_data, depending on how the post was edited.

learn more… | top users | synonyms

20
votes
6answers
10k views

Add validation and error handling when saving custom fields?

I have a function that defines a custom field on a post type. Say the field is "subhead". When the post is saved, I want to do some validation on the input, and display an error message on the post ...
14
votes
1answer
4k views

Why does save_post action fire when creating a new post?

I'm surprised by the fact that my function that I've tacked onto the save_post action fires when I click the "New Post" link in the Admin Dashboard. Note - this is before I've pressed Save or Update, ...
9
votes
3answers
5k views

Passing error/warning messages from a meta box to “admin_notices”

I have a simple meta box that updates the post custom fields (using update_post_meta()). How can I send a error or warning message to the next page after the user publishes/updates the post and ...
7
votes
2answers
2k views

Return $post_id when DOING_AUTOSAVE?

I see the following pattern over and over, on this site and on other places: add_action( 'save_post', 'wpse14169_save_post' ); function wpse14169_save_post( $post_id ) { if ( defined( ...
5
votes
2answers
2k views

Why won't my metabox data save?

I have a custom post with a lot of meta boxes. I recently tried to add some dynamic metaboxes, using this question: Create more Meta Boxes as needed I managed to get the metaboxes to add nicely, the ...
5
votes
4answers
4k views

Prevent post from being published if custom fields not filled

I have a custom post type Event that contains a starting and ending date/times custom fields (as metaboxes in the post edit screen). I would like to make sure that an Event cannot get published (or ...
4
votes
2answers
3k views

Set custom messages for post update/save

I am trying to make a custom message instead of the default message when i save a post, does anybody know how to do it!
4
votes
1answer
203 views

Saving (Updating) Post / Page Edits With AJAX

One of the major frustrations while editing posts/pages is that after saving, two things happen: The admin page is reloaded, which takes time More importantly, the cursor position is lost If ...
3
votes
4answers
3k views

Force post slug to be auto generated from title on save

I'm running a property site where many properties are sold in apartment blocks. Because of this what the content editors do is create a post/property with all the details and then use a duplicate ...
3
votes
2answers
3k views

Retrieve 1st image in post and set it as featured image, when post is saved/updated

I need to set a featured image for every post I publish. So, when a post is published/updated, the script would scan the post for the first image using a script like this: // Get URL of first ...
3
votes
2answers
2k views

How to avoid infinite loop in save_post callback

I have been using this site alot for solving my problems, but this time I did not succeed in finding and answer for my problem. I get and infinite loop when using wp_update_post inside a function ...
3
votes
2answers
341 views

Disable the post save process completely

I need a way to disable the save process completely using an action/filter. Something that works (for e.g.:) in the query, posts_clauses or wp_insert_post/save_post/update_post hooks. So far I only ...
3
votes
2answers
707 views

Submitting posts from the front end - sanitizing data

I have a form that allows any registered user to submit a post (it's a custom post type - forum related), and I'm using wp_insert_post() to insert it into the db. From what I've seen in this function ...
3
votes
1answer
68 views

Disallow a user to post in certain categories

I am writing a plugin that will disallow user to post in certain categories. I am trying to display an error message to the user when he tries to post in the restricted category and also want WP ...
3
votes
2answers
644 views

Why can't I hook into save_posts after admin_init?

I'm trying to hook into the "save_post" action from an AJAX callback in my plugin, but it doesn't seem to work. In fact, hooking into "save_posts" only seems to work from a few key action execution ...
3
votes
1answer
747 views

Action hook 'save_post' triggered when deleting posts

I developed a custom plugin which rewrite some content of my posts, but when i move a post to trash, the action hook 'save_post' is triggered and the post is not deleted. A simplified version of my ...
3
votes
0answers
227 views

Have save_post write to database image meta [closed]

Programming rookie back again. I have been studying the add_action('save_post','') but I am having difficulty getting it to work for my needs. The concept is simple - update the database information ...
2
votes
2answers
172 views

How to force function to run as the last one when saving the post?

I don't know how to force this function to run as the last function after all other save things from WP are executed. This is my current code in my plugin: add_action( 'save_post', 'do_custom_save' ...
2
votes
2answers
1k views

Using save_post to replace the post's title

I am using custom posts, and in these, I don't a need for the title. This causes Wordpress to set the titles of my posts to "Auto Draft". I'd like to change the title's value to something else, ...
2
votes
1answer
1k views

Check before publishing, if already exist post with current custom field value

I have a custom post type with only 3 custom fields in it. $post_types = get_post_meta($post->ID,'post_types',true); $post_taxonomies = get_post_meta($post->ID,'post_taxonomies',true); ...
2
votes
1answer
450 views

Why does publish_{custom-post-type} fire on update?

I'm trying to fire two different actions on both publish and update. Neither should ever fire at the same time. The problem is that publish_pm_task is getting fired on update and save_post is never ...
2
votes
2answers
456 views

How to verify nonce from Bulk/Quick Edit in save_post?

I need to do something specific in my save_post callback for bulk and/or quick edits, so how do I check the nonce for that? I can only find _wpnonce in the edit.php html source, but can't find an ...
2
votes
3answers
2k views

A Post is saved twice or more during add_action(save_post)

I have added a custom meta box for advanced information for a specific post category, to my create new post page. Now I recognize, that if I save the post, it writes 2 entries in the db with 2 ...
2
votes
3answers
4k views

Check for update vs new post on save_post action

Is it possible within the save_post action to determine whether it's a new post being created or an existing post being update?
2
votes
1answer
717 views

Post Formats in the context of the Database 'Save Post' function

I am trying to pick up an in-depth understanding of how post-formats relate to posts 'in the database' - i.e. what meta information when compiled together constitutes a post format. And yes, I do ...
2
votes
1answer
69 views

How to stop a user from updating the post date

I have a custom role, called "dealer". Once they have posted a new post, they cannot delete it. It's important they cannot update the date either, and I'm a little stuck on how to do this. It looks ...
2
votes
1answer
1k views

save_post + insert_post = infinite loop

I'm trying to automatically create a new post when a post containing a certain custom field is saved - hooking an insert_post function into save_post. This generates an infinite loop. I found some ...
2
votes
1answer
203 views

unable to save post meta on single field with multiple selects

I set up two select boxes in one meta field by putting two arrays inside the 'options' array of my callback. The HTML is outputting fine, but the meta is not being saved to the database... Here is a ...
2
votes
2answers
151 views

How to check what kind of saving it is?

I use save_post hook, and I have to know inside this hook function, whether it is a publishing ( draft to publish ) or is it an updating, like publish to publish or draft to draft. Is there a way to ...
2
votes
1answer
1k views

Why is my custom meta box input not saving

I've added the following function to functions.php to add a custom meta box enabling the end user to input a gallery shortcode. I then intend to use this to place a gallery in a specific place on the ...
2
votes
2answers
416 views

Trigger “unsaved changes” dialog for custom post meta changes

I'm using a custom post type with custom meta fields, but autosave and the "unsaved changes" dialog don't seem to be triggered for these custom meta fields. Autosave isn't as important to me as the ...
2
votes
1answer
230 views

Frontend posting - everything saves other than checkboxes?

I have a form on the frontend of Wordpress which is saving all fields except a checkbox array. It displays and saves the correct values if I tick the boxes in the backend post editor, and the ticks ...
2
votes
1answer
348 views

How to assign default taxonomy to pages on 'save_post'?

I try to assign custom taxonomies to a page when newly added by the "publish" button. This is the function: function set_default_object_terms( $id, $post ) { if ( 'publish' === $post->post_status ...
2
votes
1answer
841 views

Post Auto Draft Issue

I've made custom post type without title and editor and I use custom taxonomies, custom fields and attachments to form a post. My title is generated from custom taxonomies and custom fields (or "ID #" ...
2
votes
2answers
142 views

Placement of Code in Plugin for hooking `save_post`

I ran into a strange Problem today developing a new Plugin. I set it up as usual, creating the f711-roomprice folder in the Plugindirectory, and creating the f711-roomprice.php as well as an inc ...
1
vote
1answer
734 views

Get the post_id of a new post

There are several ways to get the id of a post after it has been saved (auto, etc.), but is there a way to get the post id immediately after you create a new post? I am trying to create a directory ...
1
vote
2answers
1k views

How can I pass $post object to 'save_post' add_action?

I have several functions that are called from inside a save_post function. However, all of the functions that use the $post object are returning incorrect values because it appears that the default ...
1
vote
3answers
303 views

Is it possible to create a post using a metabox?

I am trying to create a post (well, in the end, multiple posts) from a metabox using the 'save_post' hook. I executed the script once, creating 3900 or so new posts before I crawled into posts.php and ...
1
vote
3answers
2k views

Conflicting save_post functions when passing the post id and saving custom meta boxes for different post types

Post save functions are conflicting with each other when adding them to the save_post action hook. 2 different custom post types with 2 different (one for each post type) custom meta boxes. I'm ...
1
vote
1answer
577 views

Remove empty lines ( ) when author updates their post

Whenever adding an empty line between paragraphs using TinyMCE, the   character entity is added. How can I strip the content of all instances of this character whenever an author updates ...
1
vote
1answer
428 views

save_post action firing before I publish / save the post

I'm trying to update a posts meta only after it has been saved or updated. My function is pretty simple function update_test( $post_id ) { update_post_meta($post_id, 'copied', '1'); ...
1
vote
1answer
221 views

Better post meta efficiency?

I work with websites which require using Post Meta for Post Objects within a particular Post Type. I often add a Meta Box to a particular Post Type, to provide additional settings for the new Post ...
1
vote
1answer
272 views

using new WP_Query in save_post function alters $post

I'm trying to include a WP_Query in a save_post function for a custom post type. The query is used to check all the other posts for a custom field bh_shortcode to make sure the content of the field is ...
1
vote
1answer
279 views

Faking the “onSave” event

While a specific use-case, I believe this may benefit others. I am using TurboCSV to mass-import a number of posts into WordPress (which it is doing wonderfully so far). Within that import, I am ...
1
vote
1answer
183 views

WordPress as a web app - always auto-save post and meta data

Is is possible to make WordPress auto-save revisiona more often and have the auto-save version always be the most recent version - Like the way google docs works. This would mean the update button is ...
1
vote
2answers
704 views

Metabox saves on Update or Publish, but not on Saving Draft

I have created custom post types that also have custom meta_boxes I've created. Currently, they save when I publish or update a post, but they don't save when I'm in draft mode making changes. ...
1
vote
1answer
884 views

update_post_meta() whenever custom post type is updated

I need the following functionality. Whenever my custom post type is updated or saved I need to overwrite certain custom post metas. I need to make sure this only affects posts of the post type ...
1
vote
1answer
74 views

Change 2 fields in the post depending on the 3rd field before save

I have a custom content type called cards. I have 3 custom meta fields called: my_cards_activity (select field type; options 0 and 1) my_cards_user (select type; many options) my_cards_datetime ...
1
vote
2answers
116 views

How to run a function once on every post in database?

I have a simple function that automatically formats the whole post after publishing/updating. It's hooked on: add_action('save_post', 'myFunction'); I would like to format every post, that I ...
1
vote
1answer
1k views

Save selected item from dropdown menu in a meta box as a metadata value for a custom post type

I have been beating my brains on this one for days now and my Google-fu is failing me. I have created a custom meta box with a dropdown menu that lists all of the terms from a custom taxonomy. What I ...