Link to home
Start Free TrialLog in
Avatar of igloobob
igloobobFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Automatically assign a Wordpress Custom Taxonomy Term to all posts in a Custom Post Type

Hello,

I have a custom post type on a wordpress site with many posts in. I have created a new Taxonomy and I need to assign one of the Terms of that taxonomy to all posts in a Custom Post Type. The taxonomy is registered to that CPT and I could do this manually but there are loads so would take forever.

I think something with wp_set_object_terms should do it but I'm not sure.

So I want to be able to set this term to update all existing posts within a custom post type, and also make sure that any new posts in that CPT are automatically tagged with this taxonomy theme.

This taxonomy has several terms and I would want to assign one term to each of several CPTs in my site automatically. All CPTs are registered with the taxonomy.

Can anyone help please?

Thanks!
Avatar of igloobob
igloobob
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Just realised there's a batch update in the CPT list page so I've updated all existing posts now.

So what I need is to set up so that any new posts in that CPT are automatically tagged with this taxonomy theme rather than having to rely on admin remembering to add the term manually each time.
ASKER CERTIFIED SOLUTION
Avatar of eemit
eemit
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello.

I am so sorry I never replied to this. Someone else took over the job and I'll be honest I completely forgot about this thread.

Anyway, I'm just doing some housekeeping on my account so just had a look at this. I gave it a go on a dev site just to test it for future reference.

I added a custom post type called 'houses'. And a taxonomy called 'house-type' with the terms 'detached' and semi-detatched'.

It doesn't seem to work for me, though I have probably done something wrong. Is there somewhere in this code I am supposed to enter the Custom Post Type name that the taxonomy is for? Is that what I am missing I wonder?

add_action( 'save_post', 'myprefix_set_default_object_terms', 11, 2 );
function myprefix_set_default_object_terms( $post_id, $post ) {
	if ( 'publish' === $post->post_status ) {
		// Change here your custom taxonomy slugs and term slugs
		$defaults = array(
			'house-types' => array( 'semi-detached', 'detached' )
		);
		$taxonomies = get_object_taxonomies( $post->post_type );
		foreach ( (array) $taxonomies as $taxonomy ) {
			$terms = wp_get_post_terms( $post_id, $taxonomy );
			$append = ! empty( $terms );

			if ( array_key_exists( $taxonomy, $defaults ) ) {
				// usage: wp_set_object_terms( $object_id, $terms, $taxonomy, $append );
				$term_taxonomy_ids = wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy, $append );

				if ( is_wp_error( $term_taxonomy_ids ) ) {
					// There was an error somewhere and the terms couldn't be set.
				} else {
					// Success! These terms were added to the post.
				}					
			}
		}
	}
}

Open in new window


Again, many apologies for my rudeness in forgetting this thread.
Hi igloobob,
here works well.
Any new posts in that CPT are automatically tagged with these taxonomy terms.
Note that this works only by saving your CPT.
You can also use another version of 'save_post' action: save_post_{$post->post_type}
so instead of:
add_action( 'save_post', 'myprefix_set_default_object_terms', 11, 2 );

Open in new window

Use this:
add_action( 'save_post_houses', 'myprefix_set_default_object_terms', 11, 2 );

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks eemit, will have a go with this later on today and report back
just had a play and it works great, thanks!