Created
July 2, 2014 19:59
-
-
Save bjorn2404/276741cce5cacd35a4ab to your computer and use it in GitHub Desktop.
Remove duplicate WordPress post meta
This file contains hidden or 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
<?php | |
require_once('../../../wp-load.php'); //File was temporarily placed in the theme | |
define( 'WP_DEBUG_DISPLAY', true ); | |
ini_set( 'display_errors', true ); | |
$allposts = get_posts('numberposts=-1&post_type=guide&post_status=any'); //Post query | |
$keys = array('address', 'address2', 'city', 'state', 'zip'); //Add post meta keys here | |
foreach ( $keys as $key ) { | |
foreach( $allposts as $postinfo) { | |
// Fetch array of custom field values | |
$postmeta = get_post_meta($postinfo->ID, $key); | |
//print_r($postinfo); | |
if (!empty($postmeta) ) { | |
// Delete the custom field for this post (all occurrences) | |
delete_post_meta($postinfo->ID, $key); | |
// Insert one and only one custom field | |
update_post_meta($postinfo->ID, $key, $postmeta[0]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what?query all posts to check duplicate?is it a good idea?