Solved

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

Posted on 2014-09-23
7
79 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you think that WordPress is just for blogs?  Think again!  WordPress is really a fantastic all around platform that you can use to develop websites on.  Integrated into its basic functionality is the ability to create pages using your choice of a…
In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…

828 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