admin管理员组文章数量:1432573
I wanted to save custom field values to the revision history. The following URL had code that helped me achieve that (/).
The problem is that I have to click the "Update" button twice in order for it to show up on the revisions history. Clicking "Update" once does update values correctly, it just does not show in revision history unless I click it twice.
Here is the code:
function ca_members_fields(){ global $CustomPress_Core; $member_fields=$CustomPress_Core->get_custom_fields_set('book'); return $member_fields; }
add_filter( '_wp_post_revision_fields','ca_member_fields', 10, 1 );
function ca_member_fields( $fields ) {
$members_fields=ca_members_fields();
foreach($members_fields as $fieldname=>$members_field){
$fields['ct_'.$fieldname] = $members_field['field_title'];
}
return $fields;
}
function ca_field( $value, $field_name,$post ) {
$members_fields=ca_members_fields();
foreach($members_fields as $fieldname=>$members_field){
if($field_name==$fieldname){
$value= get_metadata( 'post',$post->ID, 'ct_'.$fieldname , true );
}
}
return $value;
}
function ca_custom_admin_head() {
$members_fields=ca_members_fields();
foreach($members_fields as $fieldname=>$members_field){
add_filter( '_wp_post_revision_field_'.'ct_'.$fieldname, 'ca_field', 10, 4 );
}
}
add_action( 'admin_head', 'ca_custom_admin_head' );
add_action( 'save_post','ca_save_member_revision', 10, 2 );
function ca_save_member_revision( $post_id, $post ) {
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$parent = get_post( $parent_id );
if($parent->post_type='book'){
$members_fields=ca_members_fields();
foreach($members_fields as $fieldname=>$members_field){
$meta = get_post_meta( $parent->ID, 'ct_'.$fieldname, true );
if ( false !== $meta ){
add_metadata( 'post', $post_id, 'ct_'.$fieldname, $meta );
}
}
}
}
}
add_action( 'wp_restore_post_revision', 'ca_restore_revision', 10, 2 );
function ca_restore_revision( $post_id, $revision_id ) {
$post = get_post( $post_id );
$revision = get_post( $revision_id );
if($post->post_type='book'){
$members_fields=ca_members_fields();
foreach($members_fields as $fieldname=>$members_field){
$meta = get_metadata( 'post', $revision->ID, 'ct_'.$fieldname, true );
if ( false !== $meta ){
update_post_meta( $post_id, 'ct_'.$fieldname, $meta );
}
}
}
}
本文标签: post metaCustom Field Values not updating unless I click quotUpdatequot twice
版权声明:本文标题:post meta - Custom Field Values not updating unless I click "Update" twice 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745582220a2664687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论