Created
January 12, 2017 15:37
-
-
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
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
<?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