最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

actions - Run a function when a custom post is update?

programmeradmin0浏览0评论

There is a custom post type. Each post has a repeater field. When a post is updated, I need to get a value from the repeater field and insert it into a custom table in the WordPress database. To do that I want to run a function when a post is updated. I have tested three 3 hooks for that. I tested post_updated, save_post and wp_insert_post. Neither of those is working. Here is my code:

For wp_insert_post:

function insert_values( $post_id, $post ) {
print_r($post);
if ( $post->post_type == 'product' ) {
    if( $post->post_date != $post->post_modified ){
        echo "Post is updated";         
    }
    else {
        echo "Post is created";     
    }
  }
}
add_action( 'wp_insert_post', 'insert_values', 10, 2 );

For post_updated :

function check_values($post_ID, $post_after, $post_before){
    echo '<b>Post ID:</b><br />';
    var_dump($post_ID);

    echo '<b>Post Object AFTER update:</b><br />';
    var_dump($post_after);

    echo '<b>Post Object BEFORE update:</b><br />';
    var_dump($post_before);
 }
 add_action( 'post_updated', 'check_values', 10, 3 );

For save_post:

function test_save_hook( $post_id, $post, $update ){
    $updated = ($update) ? "updated" : "saved";
    echo "Post type " . $post->post_type . " was " . $updated;
}
add_action( 'save_post', 'test_save_hook', 10, 3 );

Those 3 hooks are not working. It only works when a post is created.

发布评论

评论列表(0)

  1. 暂无评论