admin管理员组文章数量:1332339
I've a custom field in a custom post type whose value I want to calculate dynamically before publishing the post itself for the first time and this custom field gets stored in the postmeta
table. How can I achieve this desired functionality to edit or modify post_meta
data before saving or publishing the post ?
Thanks
I've a custom field in a custom post type whose value I want to calculate dynamically before publishing the post itself for the first time and this custom field gets stored in the postmeta
table. How can I achieve this desired functionality to edit or modify post_meta
data before saving or publishing the post ?
Thanks
Share Improve this question edited Jun 21, 2020 at 18:52 Mort 1305 9835 silver badges18 bronze badges asked Jun 21, 2020 at 17:53 DavidGDavidG 12 bronze badges1 Answer
Reset to default 0You can use save_post
action which gets triggered when a post is created or updated.
https://developer.wordpress/reference/hooks/save_post/
In your function, you will have to check for your custom post type, set the value you would like to have to a variable, and pass it to the update_post_meta
function with giving the name of your custom field.
function my_update_on_save( $post_id ) {
if ( get_post_type($post_id) == 'your_custom_post_type' ) {
// Do nothing if this is a post revision
if ( wp_is_post_revision( $post_id ) )
return;
$value = 'your value';
update_post_meta($post_id, 'your_custom_field_name', $value);
}
}
add_action( 'save_post', 'my_update_on_save', 10, 2 );
本文标签: phpHow to edit post meta data before publishing the post it self wordpress
版权声明:本文标题:php - How to edit post meta data before publishing the post it self wordpress? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742329367a2454375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论