Skip to content

Instantly share code, notes, and snippets.

@bjorn2404
Created July 2, 2014 19:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjorn2404/276741cce5cacd35a4ab to your computer and use it in GitHub Desktop.
Save bjorn2404/276741cce5cacd35a4ab to your computer and use it in GitHub Desktop.
Remove duplicate WordPress post meta
<?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]);
}
}
}
@xkungfu
Copy link

xkungfu commented Aug 28, 2017

what?query all posts to check duplicate?is it a good idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment