Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Forcing A Post To Use A Specific Category and/or Tag WordPress

  • SOLVED

I'm setting up a WP Multisite (aka WordPress MU, WPMU, etc.) install where all the sub-blogs on the network will use one common theme, the blog users have editor rights only (can't change the theme, plugins, etc.)

I'm creating three Custom Content Types (aka Custom Post Types) each Custom Post Type will have its own hierarchical Custom Taxonomy each with one Category (only) that is "global" (meaning common to all sub-blogs on the network).

Example:

Custom Post Type = "Vendor News" ---> Custom Taxonomy = "News"

Custom Post Type = "Product News" ---> Custom Taxonomy = "Product"


We need to preserve the Tag taxonomy type for each of these Custom Post Types for allowing sub-blog owners to tag their posts for further sorting/discovery within the Main Site

Getting to my question (finally):

Is there a way to "force" a Custom Post Type to use a specific Category from an associated Custom Taxonomy for that post type -- sort of the way a regular WP install settings stipulates a "default category" for "uncategorized" posts

We need each of these three Custom Post Types to use one specific Category unique to that Post Type

We're doing this to streamline the admin for very "un-tech-savvy" (casual) users.

I'm specifically looking for a plugin or code that will work in an MU environment and can be "set" once for all new sub-blogs created on the network -- set once and done as a default for all new sub-blogs created.

Answers (3)

2010-09-13

Michael Fields answers:

Great Question!
Here's my solution:
function mfields_set_default_object_terms( $post_id, $post ) {
if( 'publish' === $post->post_status ) {
$defaults = array(
'post_tag' => array( 'taco', 'banana' ),
'monkey-faces' => array( 'see-no-evil' ),
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'mfields_set_default_object_terms', 100, 2 );


You will need to configure the $defaults array to reflect your wishes. The keys are taxonomy slugs and the values consist of an array that contains term slugs from that taxonomy... you can list as many as you want to be set by default. This code is post_type independent meaning that as long as the taxonomy exists and is registered for the post_type being saved, it should work. No need to confgure information for individual post_types.

Hope this helps,
-Mike


Scott McIntyre comments:

Hey Michael,

I think this is what I need -- but would you mind kind of "walking me through it" -- dumbed down a bit - I suck at PHP -- I'm getting the gist, just need to wrap my head around it to be clear. Thanks. Scott


Michael Fields comments:

Sure, no problem. The part that you need to edit is the $defaults variable. This is a multi-dimensional array that basically assigns a list of default terms to a taxonomy. It uses only slugs.

If you wanted to set a default term for the pie-type taxonomy, your $defaults might look something like this:
$defaults = array(
'pie-types' => array( 'cherry' ),
);


I created the code to be easy to configure. If you had a locations taxonomy and wanted to default to the west coats, your defaults array might look something like this:

`$defaults = array(
'locations' => array( 'oregon', 'washington', 'california' ),
);`

In this example, all three terms will be associated upon save if no terms were added by the author.

Hope this helps,
-Mike

2010-09-13

Duncan O'Neill answers:

// CREATE YOUR POST TYPE

add_action( 'init', 'create_my_post_types' );

function create_my_post_types() {
register_post_type( 'super_duper',
array(
'labels' => array(
'name' => __( 'Super Dupers' ),
'singular_name' => __( 'Super Duper' ),
'parent' => __( 'Parent Super Duper' ),
),
'public' => true,
'taxonomies' => array( 'post_tag', 'category '),

)
);
}

The above code ( updated a bit ) from here;

http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress

and you might also find this useful, "Post types and taxonomies: Linking terms to a specific post";

http://justintadlock.com/archives/2010/08/20/linking-terms-to-a-specific-post


Scott McIntyre comments:

Thanks Duncan,

I understand how to create custom post types and custom taxonomies (do it all the time) -- what I'm trying to figure out is: when the user adds a particular custom post type, how can I set it up so that post uses a default category that I specify uniquely to that custom post type.

My original question may have been unclear

So...

If I create a custom post type called: "Pies" and I want all posts added under this custom post type to use the category "Cherry" under the custom taxonomy "Pie Types" how would I achieve this?

2010-09-13

Denzel Chia answers:

Hi,

Please try out the following codes, put them in your functions.php
I had tried them with my own custom post types.
The codes are commented with explanations.
Basically, it's a function hooked into save post action hook, which will be fired upon publishing of content. This function will look for user checked category under custom taxonomy. The Function will auto set the default category if user did not select any.


function add_default_custom_post_cat(){
global $post;
$default_cat = 'false';

//check the post type, whether its our custom post
$checkpost = $post->post_type;

//proceed if post type is Pies
if($checkpost=='Pies'){

//assign post id
$post_ID = $post->ID;

//get all post terms under taxonomy Pie Types, which the user has checked.
$tags = wp_get_post_terms( $post_ID,'pie-types' );

//if there is no tags checked by user we will set the default cat
if(empty($tags)){
$default_cat = 'false';
}

if(!empty($tags)){
foreach($tags as $tag){
//check if user has checked cherry category
//we do nothing
//if user has checked categories but not cherry
//the $default_cat will remain false
if($tag->slug=='cherry'){
$default_cat = 'true';
}
}
}//end if(!empty($tags))

//assign the category id of cherry, assume its 33
//You will need to add category cherry under Pie types taxonomy and find out the id
$cat_ids = array( 33 );

//check $default_cat is false, if user did not select
//we set default term as cherry! for taxonomy pie types!
if($default_cat=='false'){
wp_set_object_terms( $post_ID, $cat_ids, 'pie-types' );
}


}//end if($checkpost=='Pies')

}
//add into action hook save post which will be fired when post are published!
add_action('save_post', 'add_default_custom_post_cat');


Hope it works for you!

Thanks.