Skip to content

Instantly share code, notes, and snippets.

@jorenvh
Created January 12, 2017 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorenvh/b8fece7131f51214730825bbbe59bb2c to your computer and use it in GitHub Desktop.
Save jorenvh/b8fece7131f51214730825bbbe59bb2c to your computer and use it in GitHub Desktop.
Update custom taxonomy field for ACF plugin to recognise existing terms on existing posts
<?php
add_action('init', 'td_add_special_terms_of_existing_posts_to_custom_field');
function td_add_special_terms_of_existing_posts_to_custom_field()
{
$postTypes = ['post', 'external_urls', 'videos'];
$custom_query_args = array(
'post_type' => $postTypes,
'posts_per_page' => -1,
'orderby' => 'post_date_gmt',
'order' => 'DESC',
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
$terms = get_the_terms(get_the_ID(), 'specials');
if($terms) {
$terms = wp_list_pluck( $terms, 'term_id' );
}
if($terms) {
update_field('specials', $terms, get_the_ID());
}
endwhile;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment