admin管理员组文章数量:1404927
I want to update one of my meta field and redirect and I am using post_updated
hook. 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_updated
hook. 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 ?
- 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
1 Answer
Reset to default 1save_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
版权声明:本文标题:WordPress hook after post content and meta update 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744875687a2629902.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论