Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress function to auto-populate a taxonomy with a custom post type's entries.
function custom_tax_init(){
//set some options for our new custom taxonomy
$args = array(
'label' => __( 'My Custom Taxonomy' ),
'hierarchical' => true,
'capabilities' => array(
// allow anyone editing posts to assign terms
'assign_terms' => 'edit_posts',
/* but you probably don't want anyone
* except admins messing with what
* gets auto-generated! */
'edit_terms' => 'administrator'
)
);
// create the custom taxonomy and attach it to a custom post type
register_taxonomy( 'my-taxonomy', 'post-type-A', $args);
}
add_action( 'init', 'custom_tax_init' );
function update_custom_terms($post_id) {
// only update terms if it's a post-type-B post
if ( 'post-type-B' != get_post_type($post_id)) {
return;
}
// don't create or update terms for system generated posts
if (get_post_status($post_id) == 'auto-draft') {
return;
}
/*
* Grab the post title and slug to use as the new
* or updated term name and slug
*/
$term_title = get_the_title($post_id);
$term_slug = get_post( $post_id )->post_name;
/*
* Check if a corresponding term already exists by comparing
* the post ID to all existing term descriptions.
*/
$existing_terms = get_terms('my-taxonomy', array(
'hide_empty' => false
)
);
foreach($existing_terms as $term) {
if ($term->description == $post_id) {
//term already exists, so update it and we're done
wp_update_term($term->term_id, 'my-taxonomy', array(
'name' => $term_title,
'slug' => $term_slug
)
);
return;
}
}
/*
* If we didn't find a match above, this is a new post,
* so create a new term.
*/
wp_insert_term($term_title, 'my-taxonomy', array(
'slug' => $term_slug,
'description' => $post_id
)
);
}
//run the update function whenever a post is created or edited
add_action('save_post', 'update_custom_terms');
@She-Codes

This comment has been minimized.

Copy link

@She-Codes She-Codes commented Apr 2, 2014

This was exactly what I needed thanks so much for sharing!

@fredbradley

This comment has been minimized.

Copy link

@fredbradley fredbradley commented May 1, 2014

You little legend! This is what I was looking for! :)

@robertirish

This comment has been minimized.

Copy link

@robertirish robertirish commented May 21, 2014

Thanks for this! Elegant solution.

@thethe

This comment has been minimized.

Copy link

@thethe thethe commented Jun 29, 2014

Thanks a lot! Is it possible to auto select this created taxonomy?

@UnionDesign

This comment has been minimized.

Copy link

@UnionDesign UnionDesign commented Nov 25, 2014

Awesome. But how would one modify this to take into account deleted posts (i.e. delete the corresponding term when the post is deleted). I tried. No luck.

@burnified

This comment has been minimized.

Copy link

@burnified burnified commented Feb 16, 2015

I could see why the auto delete feature was left out. I mean, it would just be another function you could call to do it, but if we're this deep into integration, there's a reason you wouldn't want that to happen. Most of the time it's not a one-way thing.

http://wordpress.stackexchange.com/questions/136103/dynamically-create-remove-terms-in-taxonomy-when-custom-post-type-is-published-t

@dennisaskeland

This comment has been minimized.

Copy link

@dennisaskeland dennisaskeland commented Oct 22, 2015

One up for auto delete function to be added. And whole snippet moved to a plugin with dynamically changeable CPTs, thumbs up

@opolanco23

This comment has been minimized.

Copy link

@opolanco23 opolanco23 commented Sep 25, 2017

if anyone is interested I wrote a simple addition to this script that will create a custom meta field for your taxonomy, I named it post_id but you can name it whatever you like and have the script save to that custom field instead of the taxonomies description.

@moneya

This comment has been minimized.

Copy link

@moneya moneya commented Jan 29, 2018

Any idea on how to auto-populate a taxonomy with another custom taxonomy entries ?
@dennisaskeland @burnified @brenna

@moneya

This comment has been minimized.

Copy link

@moneya moneya commented Jan 29, 2018

Any idea on how to auto-populate a taxonomy with another custom taxonomy entries ?
@dennisaskeland @burnified

@pcopu

This comment has been minimized.

Copy link

@pcopu pcopu commented Nov 19, 2018

For anyone interested, you can auto select the created taxonomy by replacing this code:

wp_insert_term($term_title, 'location', array(
    'slug' => $term_slug,
    'description' => $post_id
    )
);

With this code:

wp_set_object_terms( $post_id, $term_title, $tax, false );

The last argument is to set whether you're appending or replacing. Set it false to replace any currently selected terms.

@badfeather

This comment has been minimized.

Copy link

@badfeather badfeather commented May 16, 2019

I love this solution for generating taxonomy terms based on post types, however I was looking for a solution that didn't rely on term descriptions, since I needed to use those in my theme. I ended up using get_term_meta() and update_term_meta() instead of term descriptions.

Modified function below:

    foreach($existing_terms as $term) {
	$related_post_id = get_term_meta($term->term_id, 'related_post_id', true);
        if ($related_post_id == $post_id) {
            //term already exists, so update it and we're done
            wp_update_term($term->term_id, 'my-taxonomy', array(
                'name' => $term_title,
                'slug' => $term_slug
                )
            );
            return;
        }
    }

    /* 
    * If we didn't find a match above, this is a new post, 
    * so create a new term.
    */
    wp_insert_term($term_title, 'my-taxonomy', array(
        'slug' => $term_slug,
    ) );

    /* 
    * Add term meta to new term 

    */    
    $new_term = get_term_by('slug', $term_slug, 'my-taxonomy');
	if ( $new_term ) {
		update_term_meta($new_term->term_id, 'related_post_id', $post_id);
	}
@samabp

This comment has been minimized.

Copy link

@samabp samabp commented Sep 24, 2019

@badfeather Could you please explain why you had to go this route?

@badfeather

This comment has been minimized.

Copy link

@badfeather badfeather commented Sep 25, 2019

@samabp Sure. I needed to use category/term descriptions for what they're intended for (writing a description of that category/term). It seemed to make more sense to take advantage of taxonomy term meta to save these relationship ID values, rather than hacking them into the description field.

@samabp

This comment has been minimized.

Copy link

@samabp samabp commented Sep 25, 2019

@badfeather Thanks a lot, makes sense to me.

@caesarsamsi

This comment has been minimized.

Copy link

@caesarsamsi caesarsamsi commented May 18, 2020

Is there a way to use woocommerce product attributes (which is a custom taxonomy) with this code?

Thanks!

@brenna

This comment has been minimized.

Copy link
Owner Author

@brenna brenna commented May 19, 2020

hi @caesarsamsi, I wrote this a very long time ago and am not familiar with woocommerce so I'm not sure if that's possible or not. good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.