any ideas to get me on the right track here?
So is this just not possible in wordpress? You can not set categories automatically?
$category_ids = array(4, 5, 6);
wp_set_object_terms( $post_id, $category_ids, 'category');
I have no idea if this would work, I’m guessing here, but in that array, you would put the categry ID of the categories you want automatically assigned
instead of $post_id you would use the name you registered your custom post type as, for instance, I have a custom post type of Products, I registered as ve_products here
register_post_type( 've_products'
so for me, if I wanted category 9 assigned to my Products category I would try:
$category_ids = array(9);
wp_set_object_terms( 've_products', $category_ids, 'category');
again, I’m just thinking out loud here, I’ve never tried this, but it’s how I would experiment
Thanks so much for the explanation!
I wan on the thinking that, and now I dont know why I was, but that the cat array was the cat I wanted to assign, but I had to put the id of the posts, and as I did not know what each posts Id would be, i couldnt get it to work.
Thanks again. I just needed to read a simplified version of what the code was doing.
http://codex.wordpress.org/Function_Reference/wp_set_object_terms
hopefully it works for ya, here’s the codex page explaining the function
I read that, and then googled wp_set_object_terms a bit to try to figure it out
did you get this working godonholiday?
I am trying to do the same thing but it seems wp_set_object_terms will only take a postID.
holy shit, i just figured it out… by piecing together other bits i have found I compiled this to go in functions.php.
function add_housecategory_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
$housecat = array (4);
wp_set_object_terms( $post_ID, $housecat, 'category');
}
}
add_action('publish_houses', 'add_housecategory_automatically');
in this example my post_type is ‘houses’. The key is the hook in add_action
should be publish_your-custom-post-type
the $houscat array is the categories (separated by commas) that you want assigned. This function will assign the category when published and will ignore revisions.
I used More Types http://wordpress.org/extend/plugins/more-types/ to setup my post_types and one pleasant surprise was that when I diasbled the category taxonomy box from the post type in question, the category was still set and work in a query. However, and this is a big however, I am not sure if this was only because when I initially set up my post types, I had category enabled. You will have to test but I think it would work regardless.
I found a ton of threads about this issue so I hope everyone who needs this finds it.
Thanks guys, this had been really useful! Also note that the “get_sidebar()” function will not print out the default category being used by a custom post type, until you create a new post with the default CPT category.
glad to help
PS you might need to allow empty categories to show if you want the above sidebar issue to happen.
I thought I’d share a tweak that only adds a category if the post doesn’t have a category yet:
function add_stiwti_category_automatically($post_ID) {
global $wpdb;
if(!has_term('','category',$post_ID)){
$cat = array(4);
wp_set_object_terms($post_ID, $cat, 'category');
}
}
add_action('publish_stiwti', 'add_stiwti_category_automatically');
Ian
(@ianaleksander)
mike, is there a way to tweak that code so it only adds the category if it is a certain post type? I’ve got a post type “Sketchbook” that I want to always be in the “sketchbook” category.
function add_sketchbook_category_automatically($post_ID) {
global $wpdb;
if(!has_term('','category',$post_ID)){
$cat = array(4);
wp_set_object_terms($post_ID, $cat, 'category');
}
}
add_action('publish_sketchbook', 'add_sketchbook_category_automatically');
The important difference is that the action is hooked to ‘publish_sketchbook’ rather than publish_stiwti (which was my content type name)
Ian
(@ianaleksander)
Awesome, thanks so much. That was the bit I was totally missing. For some reason thought that your content type was a wordpress standard term that I wasn’t familiar with.