Solved

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

Posted on 2014-09-23
7
80 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
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…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

726 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