I have to execute custom PHP code after new post is saved with all it’s meta data.
My question is how to achieve that? Tried with save_post action, but it executes before meta records are saved, so I cannot use it in this case.
So, how can I run my custom function after post with all related data is saved in database?
UPDATED: I tried to achieve with next code in functions.php file:
add_action( 'save_post', 'wpse41912_save_post' );
function wpse41912_save_post() {
// get info about latest added post
$args = array( 'numberposts' => '1', 'post_type' => 'post' );
$recent_posts = wp_get_recent_posts( $args );
$myFunc_latest_id = $recent_posts[0]['ID']; // id of the latest post
$myFunc_post_details = get_post($myFunc_latest_id);
print_r($myFunc_post_details);
// how to execute php code when all post meta is added?
}
Thank you in advance!
4 s
For NEW post type ‘post’ use draft_to_publish
action hook:
function fpw_post_info( $post ) {
if ( 'post' == $post->post_type ) {
// echo '<pre>'; print_r( $post ); echo '<br />';
// $meta = get_post_meta( $post->ID ); print_r( $meta ); echo '</pre>'; die();
// your custom code goes here...
}
}
add_action( 'draft_to_publish', 'fpw_post_info', 10, 1 );
In your callback function $post
is your post as WP_post
object. You’ll get post’s meta calling get_post_meta
function.
For NEW or UPDATED post type ‘post’ use publish_post
action hook:
function fpw_post_info( $id, $post ) {
// echo '<pre>'; print_r( $post ); echo '<br />';
// $meta = get_post_meta( $post->ID ); print_r( $meta ); echo '</pre>'; die();
// your custom code goes here...
}
add_action( 'publish_post', 'fpw_post_info', 10, 2 );
In this case the callback function takes two parameters!