0

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 ?

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 Dec 28 '19 at 7:41
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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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