Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 532 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
WordPress hook after adding/updating post AND after insertion of post meta?

#1
I have a custom post type called "videos", which also has a few custom fields defined. Upon creating or updating a "videos" post, I would like to run a function.

Unfortunately, this function needs the values of the post meta data for the post that I've just created, and the usual hooks (`save_post`,`publish_post`, etc.) seem to run before the post meta is inserted into the database, so it isn't available.

If I manually update the post just by clicking "Publish" without making any changes, function works properly.

**Is there a hook that fires later in the process, after all metadata has been inserted?**
Reply

#2
There is an <del>undocumented</del> hook called [`updated_post_meta`](

[To see links please register here]

) that does what I need.

It will pass 4 parameters to the hooked function: the meta ID, the object ID (same as the post ID), the meta key, and the meta value. In my hooked function I check to see if the meta key name is the field that I need the value of and, if so, it proceeds.

Here's what it looks like:

/**
* Use value of post meta for something when the post
* meta changes
* @param integer $meta_id ID of the meta data field
* @param integer $post_id Post ID
* @param string $meta_key Name of meta field
* @param string $meta_value Value of meta field
*/
function saveYouTubeInfo($meta_id, $post_id, $meta_key='',
$meta_value=''){

// Stop if not the correct meta key
if ( $meta_key != 'my_meta_field_name') {
return false;
}

// Function code goes here.
}

add_action('updated_post_meta', 'saveYouTubeInfo', 10, 4);

By the way, unlike `added_post_meta`, you do not replace `post` with the post type that you are targeting. In my case, the name of the post type is `videos`, but I still had to use `updated_post_meta` and NOT `updated_videos_meta`.
Reply

#3
The reason post meta is not available yet it is because they use save_post hook to save post meta. So, you hook is not running after post meta is saved to database.

Two solutions to your problem.

1. Get the data from $_POST

add_action('save_post', 'my_function');

function my_function($post_id){

$postmeta = $_POST['field_name'];

}

2. Use a Higher priority for your hook, so it runs after post meta is saved

add_action('save_post', 'my_function', 12 , 3);

function my_function($post_id, $post, $update){

$postmeta = get_post_meta($post_id, 'meta_key', true);

}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through