In my custom post type, once the user saves the post, is there a way to check the value of one of the fields and update it? The value I will insert will depend on the post's ID so save_post
needs to be used in case it's a new post.
Asked
Active
Viewed 4,056 times
0
-
Maybe I misunderstood something but it sounds like the answer is in your question. – Rob Apr 19 '12 at 21:45
-
Sorry but I'm trying to learn the ropes with using `add_action`. Maybe you can post something other users like myself can work with next time. – enchance Apr 19 '12 at 22:31
1 Answers
1
Yes you can have all of your data from $_POST
or global $post
after you save or update the post using save_post hook as you mentioned in your question
add_action( 'save_post', 'afterSavePost' );
function afterSavePost($pid)
{
$postId=$pid;
// or
global $post;
$postId=$post->ID;
$postTitle=$post->post_title;
// or
$postId=$_POST['ID'];
$postTitle=$_POST['post_title'];
}
You mentioned custom field
and in that case you can use
$yourCustomField=get_post_meta($postId, 'your_custom_field',true); // get a custom field
and
$yourCustomField="New value";
update_post_meta($postId, 'your_custom_field', $yourCustomField); // update a custom field

The Alpha
- 143,660
- 29
- 287
- 307
-
1I'm trying to get my head around using `add_action` but isn't modifying `$_POST` useless since the form has already been saved to the db? – enchance Apr 19 '12 at 22:28
-
-
I'm finding `get_post_meta()` does not return submitted custom fields when first creating a post, but it does when updating a post. That's a little surprising to me, so wondered if there is a different action for handling this kind of thing when posts are first created. – Jason Oct 09 '12 at 17:12
-
[Please look ate this for more](http://codex.wordpress.org/Plugin_API/Action_Reference/save_post). – The Alpha Oct 09 '12 at 18:08