Created
October 24, 2012 21:14
-
-
Save anonymous/3948932 to your computer and use it in GitHub Desktop.
Magic Fields 2 - Post Duplications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Duplicates Magic Field 2 meta data when using WPML to duplicate a post for translation | |
function duplicate_mf_meta($master_post_id, $target_post_id){ | |
global $wpdb; | |
$table = $wpdb->prefix.'mf_post_meta'; | |
$mf_metas = $wpdb->get_results("SELECT * FROM $table WHERE post_id = $master_post_id"); | |
foreach($mf_metas as $mf_meta){ | |
$master_meta_id = $mf_meta->meta_id; | |
$master_meta_value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s LIMIT 1 OFFSET %d", $master_post_id, $mf_meta->field_name, $mf_meta->group_count - 1) ); //check since there may be multiple instances | |
$target_meta_id = $wpdb->get_var( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s LIMIT 1 OFFSET %d", $target_post_id, $mf_meta->field_name, $mf_meta->group_count - 1) ); //check since there may be multiple instances | |
$mf_meta->meta_id = $target_meta_id; | |
$mf_meta->post_id = $target_post_id; | |
//Clean up and re-add post meta since WPML only handles the first value (missing on duplication) | |
$wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d",$master_meta_value, $target_meta_id) ); //check since there may be multiple instances | |
//Copy Magic Fields meta table | |
$wpdb->insert($table, (array)$mf_meta); | |
} | |
} | |
function icl_make_duplicate_filter($master_post_id, $lang, $postarr, $id){ | |
duplicate_mf_meta($master_post_id, $id); | |
} | |
add_action('icl_make_duplicate','icl_make_duplicate_filter',10,4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment