Solved

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

Posted on 2014-09-23
7
75 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Like there are many different versions of Wordpress, there are also a handful of related side projects WordPress users should be familiar with.   All of these projects are open source and GPL licensed, which means there are few restrictions to us…
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…
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 integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

831 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