Skip to content

Instantly share code, notes, and snippets.

@hearvox
Created November 24, 2017 21:24
Show Gist options
  • Save hearvox/b1cdf6777a3d5ba68df7439a8d6ba1ec to your computer and use it in GitHub Desktop.
Save hearvox/b1cdf6777a3d5ba68df7439a8d6ba1ec to your computer and use it in GitHub Desktop.
<ol>
<?php
/* Remove duplicate meta, in this case Author and Contact (custom tax) */
// Get all posts assigned a term in a specific custom taxonomy.
$tax = 'contact';
$args = array(
'post_type' => 'post',
'posts_per_page' => 1000,
'orderby' => 'ID',
'tax_query' => array(
array(
'taxonomy' => $tax,
'operator' => 'EXISTS',
),
),
);
$query = new WP_Query( $args );
foreach ( $query->posts as $post ) {
$rm_term = ''; // Var for result of wp_remove_object_terms().
$post_id = $post->ID;
$author_id = $post->post_author;
// Get post term.
$post_terms = wp_get_post_terms( $post_id, $tax, array( 'fields' => 'ids' ) );
// Get term meta holding User ID.
$term_user_id = get_term_meta( $post_terms[0], 'user_id', true ); // CF with user ID of contact.
// If Author is also the Contact.
if ( $author_id == $term_user_id ) {
// Uncomment to remove term.
// $rm_term = wp_remove_object_terms( $post_id, $post_terms[0], 'contact' );
// List results.
echo "<li>$post_id / $author_id / $term_user_id / $rm_term</li>";
}
}
wp_reset_postdata();
?>
</ol>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment