Skip to content

Instantly share code, notes, and snippets.

@kmwalsh
Last active December 7, 2018 05:00
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 kmwalsh/aff017e34ee7a73c633e7bf4a5eca779 to your computer and use it in GitHub Desktop.
Save kmwalsh/aff017e34ee7a73c633e7bf4a5eca779 to your computer and use it in GitHub Desktop.
Migrate ACF select values into taxonomy, then apply taxonomy terms to all posts
/**
* Ran into an issue with a WP site where I needed to change an ACF select dropdown into a real WordPress taxonomy. Wrote this to move the data from an ACF select to taxonomy. This will create terms from an ACF select (manually, have to get the data from the ACF field editing interface). Then it will go through all posts and apply the new taxonomy term according to the ACF select value that already exists on the post.
*
* ⚠⚠⚠ WARNING ⚠⚠⚠
*
* This is not a professional migration thing. If you use this:
* Put into `functions.php` or as your own `mu-plugin` file.
* Run on a local installation first. If you can run locally and export your DB back to live, even better.
* No idea how it'd perform if you had thousands of ACF fields. It worked OK for me with 250 potential ACF select values.
* PHP memory limits may need to be increased. There is a WP_Query that will loop through *all* of your posts. Not suggested to use on a tiny shared host.
* If you need to do this for more than one ACF field (e.g., you are migrating two different fields to two different taxonomies) -- use two different functions, and run each migration one at a time. Don't stuff three migrations into `migrate_acf_sel_to_taxonomy_add_terms_to_posts()` because it will definitely run out of memory.
*/
/**
* Migrates ACF select values into WordPress taxonomy.
*/
function migrate_acf_sel_to_taxonomy() {
$acf_types = array(
// ARRAY OF YOUR ACF LABELS HERE
// you could probably also use a function to pull the ACF labels directly out of ACF
// like so: https://support.advancedcustomfields.com/forums/topic/display-all-the-values-from-a-radio-button-field/
// but i am lazy and i just copy-pasted from the field group in wp-admin and used multi-line edits in my text editor
// it's important to make sure the values don't change -- the function below relies on the ACF select value being the
// same as the new taxonomy name
);
foreach ( $acf_types as $type ) :
// replace your taxonomy name here
$taxonomy_name = 'YOUR_TAXONOMY_NAME';
$type_nicename = $type;
// make sure that the slug does not have icky characters
// maybe not necessary, wp might do that itself?
$slug = sanitize_title($type);
wp_insert_term(
$type_nicename, // term name
$taxonomy_name, // taxonomy name
array(
'slug' => $slug,
)
);
endforeach;
}
// leave this commented out till you're ready to migrate
// then -- only uncomment it for a second, refresh an admin page, and then re-comment
// do not let this function run willy-nilly on your WP admin, will slow it down like whoa
//add_action('admin_init', 'migrate_acf_sel_to_taxonomy');
/**
* Adds new WordPress taxonomy terms to all posts, according to ACF select value already applied to post.
*/
function migrate_acf_sel_to_taxonomy_add_terms_to_posts()
{
$args = array(
'post_type' => 'post',
// NOTE: if you have tons of posts, this will probably crash your stuff
// it crashed my local php running on 1,000 posts but it was ok after i reloaded again
'posts_per_page' => -1,
);
$posts = new WP_Query($args);
$post_array = array();
if( $posts->have_posts() ) :
while( $posts->have_posts() ) : $posts->the_post();
global $post;
// replace ACF field name here
$acf_field_name = 'YOUR_FIELD_NAME';
// replace taxonomy name here
$taxonomy_name = 'YOUR_TAXONOMY_NAME';
// get_field() did not work here for me for some reason, but get_post_meta() does
$source = get_post_meta( get_the_ID(), $acf_field_name, true);
// if you ever change args don't forget hide_empty and struggle for an hour like I did 😎
$args = array(
'taxonomy' => $taxonomy_name,
'hide_empty' => false,
);
$source_terms = get_terms($args);
foreach ( $source_terms as $source_term ):
// make sure that the source acf value matches the term's name
if ( (string) $source == (string) $source_term->name ) :
wp_set_post_terms($post->ID, array($source_term->term_id), $taxonomy_name );
endif;
endforeach;
endwhile;
endif;
}
// leave this commented out till you're ready to migrate
// then -- only uncomment it for a second, refresh an admin page, and then re-comment
// do not let this function run willy-nilly on your WP admin, will slow it down like whoa
//add_action('admin_init', 'migrate_acf_sel_to_taxonomy_add_terms_to_posts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment