admin管理员组

文章数量:1404927

I want to update one of my meta field and redirect and I am using post_updatedhook. Here is my code.

function job_publish_status( $post_ID, $post_after, $post_before ) {
   $job_published_date = get_the_time("Y-m-d", $post_ID);
   $expire_date = date('Y-m-d', strtotime($job_published_date. ' + 60 days'));
   update_post_meta($post_ID, '_job_expires', $expire_date);
   $url = get_site_url();
   $url = $url.'/wp-admin/post.php?post='.$post_ID.'&action=edit';
   wp_redirect( $url );
   exit; 
}
add_action('post_updated', 'job_publish_status', 99, 3 );

It is updating this _job_expires meta field and redirecting correctly. When I edit post content and title, its updates title and content but it is not updating other meta fields in this post. Is there any hook which triggers after post content update and meta fields update ?

I want to update one of my meta field and redirect and I am using post_updatedhook. Here is my code.

function job_publish_status( $post_ID, $post_after, $post_before ) {
   $job_published_date = get_the_time("Y-m-d", $post_ID);
   $expire_date = date('Y-m-d', strtotime($job_published_date. ' + 60 days'));
   update_post_meta($post_ID, '_job_expires', $expire_date);
   $url = get_site_url();
   $url = $url.'/wp-admin/post.php?post='.$post_ID.'&action=edit';
   wp_redirect( $url );
   exit; 
}
add_action('post_updated', 'job_publish_status', 99, 3 );

It is updating this _job_expires meta field and redirecting correctly. When I edit post content and title, its updates title and content but it is not updating other meta fields in this post. Is there any hook which triggers after post content update and meta fields update ?

Share Improve this question asked Dec 27, 2019 at 22:18 wplearnerwplearner 4892 gold badges9 silver badges27 bronze badges 1
  • when you use "exit" on this action, that stop all other action then it's not a good idea to do that. why do you want to do a redirection here ? – Kaperto Commented Dec 28, 2019 at 7:41
Add a comment  | 

1 Answer 1

Reset to default 1

save_post and new_to_publish is enough, with some checks, to update post metadata. And you don't need the redirection.

<?php
/**
 * Update Postmeta.
 *
 * @param integer $post_id Post ID.
 */
function wpse355298_job_publish_status( $post_id ) {
    // Check autosave.
    if ( wp_is_post_autosave( $post_id ) ) {
        return $post_id;
    }

    // Check post revision.
    if ( wp_is_post_revision( $post_id ) ) {
        return $post_id;
    }

    // Check permissions.
    if ( 'post' === $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
    }

    $job_published_date = get_the_time( 'Y-m-d', $post_id );
    $expire_date        = date( 'Y-m-d', strtotime( $job_published_date. ' + 60 days' ) );

    update_post_meta( $post_id, '_job_expires', $expire_date );
}

add_action( 'save_post', 'wpse355298_job_publish_status' );
add_action( 'new_to_publish', 'wpse355298_job_publish_status' );

本文标签: WordPress hook after post content and meta update