6

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!

2
  • Is the post meta saved in the same function? Jun 22, 2015 at 12:54
  • Hm, not sure about your question... Please check updated part of my question, part of the function.php file
    – user198003
    Jun 22, 2015 at 13:11

4 Answers 4

6

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!

5
  • Thank you, but nothing happens by using publish_post action hook... I also tried to use added_post_meta hook but it executes after every added post meta - i need the one that executes after last added meta
    – user198003
    Jun 23, 2015 at 7:39
  • What do you mean nothing happens? Did you use the code above without un-commenting first two comments? Show me your code? Do you use it for custom post type or just for 'post'? Jun 23, 2015 at 13:37
  • Read updated answer for proper use of publish_post action hook. Jun 23, 2015 at 14:37
  • Does this work for WooCommerce Products as well?
    – Amjad
    Mar 19, 2019 at 1:07
  • A woocommerce product is a custom post type. The above solution works with any post type. Mar 19, 2019 at 3:09
6

Quite stupid solution, but works:

function afterPostUpdated($meta_id, $post_id, $meta_key='', $meta_value=''){
    if($meta_key=='_edit_lock') {
        if($_GET['message']==1) {
            //
            Your code here
            // 
        }
    }
}
add_action('updated_post_meta', 'afterPostUpdated', 10, 4);
1

You might use this:

function myFunction($post_id, $post, $update ) {
    // your code here
}

add_action('save_post', 'myFunction');

$update is a boolean and return false if this is a new post. If it's an update it returns true.

Here is the documentation: https://developer.wordpress.org/reference/hooks/save_post/

1

The correct and simpler answer is to use the wp_insert_post action.

https://developer.wordpress.org/reference/hooks/wp_insert_post/

An important distinction of wp_insert_post action is that it is fired after update_post_meta has been called.

There are 3 parameters available - the $update flag tells you if this is a new or updated post.

add_action('wp_insert_post', 'run_after_post_updated', 10, 3);    
function run_after_post_updated($post_ID, $post = null, $update = true) {
   //even though the docs say 3 parameters, it seems sometimes only the postid is passed - so you need defaults on the other 2 parameters
   $meta = get_post_meta( $post_ID ); 
   //  ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.