Solved

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

Posted on 2014-09-23
7
64 Views
Last Modified: 2015-11-02
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!
0
Comment
Question by:igloobob
  • 4
  • 3
7 Comments
 

Author Comment

by:igloobob
ID: 40338801
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.
0
 
LVL 15

Accepted Solution

by:
eemit earned 500 total points
ID: 40485999
// Try this in your child theme's functions.php
// NOTE: test this in your dev or test environment first
add_action( 'save_post', 'yourprefix_set_default_object_terms', 11, 2 );
function yourprefix_set_default_object_terms( $post_id, $post ) {
	if ( 'publish' === $post->post_status ) {
		// Change here your custom taxonomy slugs and term slugs
		$defaults = array(
			'your_taxonomy_1_slug' => array( 'term1', 'term2' ),
			'your_taxonomy_2_slug' => array( 'termA', 'termB' )
		);
		$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

0
 

Author Comment

by:igloobob
ID: 41039841
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 15

Expert Comment

by:eemit
ID: 41045190
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

0
 
LVL 15

Assisted Solution

by:eemit
eemit earned 500 total points
ID: 41045203
Also try to change your taxonomy slug called 'house-type' to 'house_type'
It should only contain lowercase letters and the underscore character.
0
 

Author Comment

by:igloobob
ID: 41173544
thanks eemit, will have a go with this later on today and report back
0
 

Author Comment

by:igloobob
ID: 41176535
just had a play and it works great, thanks!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What's this? As a volunteer Page Editor for Experts-Exchange.com, I have noticed that many authors also have blogs, and pull articles from their blogs to post at Experts-Exchange. I appreciate each author sharing their blog content with our site …
WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now