WordPress.org

Ready to get started?Download WordPress

Forums

Set category to a custom post type automatically (25 posts)

  1. godonholiday
    Member
    Posted 3 years ago #

    I have created a few custom post types:
    'Green Articles'
    'My Favs'
    'Travel'

    Each time I create a post I have to set the category (I am using categories to filter and place my posts on my home page). The category names are the same as the custom post type

    What I want to do is, once the post is published it is automatically added to the right category.

    I have seen this code, but I don't fully understand it so don't know how to customise it to my needs:
    $category_ids = array(4, 5, 6);
    wp_set_object_terms( $post_id, $category_ids, 'category');

    This goes in the functions.php file

    I would be very grateful if you could shed some light on how to achieve this.

    Thanks

  2. godonholiday
    Member
    Posted 3 years ago #

    any ideas to get me on the right track here?

  3. godonholiday
    Member
    Posted 3 years ago #

    So is this just not possible in wordpress? You can not set categories automatically?

  4. godonholiday
    Member
    Posted 3 years ago #

    Are these forums active?

  5. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    $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

  6. godonholiday
    Member
    Posted 3 years ago #

    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.

  7. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    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

  8. godonholiday
    Member
    Posted 3 years ago #

    Thanks again

  9. Taylor Baybutt
    Member
    Posted 3 years ago #

    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.

  10. Taylor Baybutt
    Member
    Posted 3 years ago #

    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.

  11. thiswayup
    Member
    Posted 3 years ago #

    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.

  12. Taylor Baybutt
    Member
    Posted 3 years ago #

    glad to help

    PS you might need to allow empty categories to show if you want the above sidebar issue to happen.

  13. Mike Stop Continues
    Member
    Posted 3 years ago #

    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');
  14. Ian
    Member
    Posted 3 years ago #

    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.

  15. Mike Stop Continues
    Member
    Posted 3 years ago #

    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)

  16. Ian
    Member
    Posted 3 years ago #

    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.

  17. Taylor Baybutt
    Member
    Posted 3 years ago #

    Hey Ian, we both were at SCAD at the same time!

  18. Ian
    Member
    Posted 3 years ago #

    Small World :D

  19. cmaier13
    Member
    Posted 3 years ago #

    Hello!

    I am having a problem with WordPress and was hoping someone could help me out. In a nutshell, I want to assign a post to a page. So that when I make a post, it shows on the home page and the Sports Page. Then if i have a different topic later on Health, i would like that post to show on the home page and on the Health page. Ideally I would like to have about 10 or so pages and then people can click on the Health tab to see all of the Articles I designated were Health. Is this possible? I've installed a few plugins, but nothing seems to be working. I'm a rookie when it comes to code, so please explain as best you can. Thanks for your help!

  20. Taylor Baybutt
    Member
    Posted 3 years ago #

    You are entirely off topic for this thread. However, you should have "sports", "health", etc. as categories. Then use the the built in menu management to add you categories to your main menu. Appearance -> menus.

    This is only available on 3.0+ and not all themes support it. If you are not a coder, you will have to find a theme that supports this. If you have any more questions, please open a new thread.

  21. cmaier13
    Member
    Posted 3 years ago #

    I'm using the Absolum theme and my website is http://www.thenewshq.com. I still don't think I explained what I was trying to do.

    If you look at the first couple of posts(picture and testing celeb), i want these posts to show on the main page, and the picture under the pictures category and the testing celeb under the celebrity news...so and and so forth.

    I added Celeb Pages and Categories to the Celeb menu, but it still is not working. Is there anything else I can do? This really is needed for functionality on this website.

    Also, if i was going to pick a new theme, how would i tell if it let me do what i want or not?

  22. noelgreen
    Member
    Posted 2 years ago #

    One really cool way you can use this code is to set up custom taxonomy for users and then have all of their posts post automatically into a category with their user name. Then you can hide that category information and not even give them the choice.

    <?php
    
    function add_blog_automatically($post_ID) {
    	global $wpdb;
    	global $current_user;
          get_currentuserinfo();
    	if(!has_term('','blog_categories',$post_ID)){
    		$cat = $current_user->user_login;
    		wp_set_object_terms($post_ID, $cat, 'blog_categories');
    	}
    }
    add_action('publish_blog', 'add_blog_automatically');
    
    ?>
  23. Kasper Isager
    Member
    Posted 2 years ago #

    Hmmm... I'm still having trouble with this. I have a CTP called "Characters" and I've registered it manually with no taxonomies. When I search my site for "Characters" though, they show up as having no category (nothing strange here). What I would like to though is to assign a category to them (Also called "Characters") so they don't show up in my search result as "Posten on (date) in" but instead with the category "Posten on (date) in Characters". I can't get these snippets you've all posted to work though... I've added

    function add_characters_category_automatically($post_ID) {
    	global $wpdb;
    	if(!has_term('','category',$post_ID)){
    		$cat = array(6);
    		wp_set_object_terms($post_ID, $cat, 'category');
    	}
    }
    add_action('publish_characters', 'add_characters_category_automatically');

    to my functions.php right after the part where I register the CPT. It doesn't assign category "6", which is "Characters" to my CPT posts -.-

  24. PatJ
    Member
    Posted 2 years ago #

    Kasper Isager:
    I think you need to register your post type WITH a taxonomy
    use
    'taxonomies' => array( 'category'),
    in your array of arguments to the register_post_type function.
    Then you can use these snippets as there has to be a taxonomy to set.

    If you don't want the categories to show up in edit post use

    function xxx_remove_metaboxes() {
    remove_meta_box( 'categorydiv', 'characters', 'normal' );
    }
    add_action('do_meta_boxes', 'xxx_remove_metaboxes');

    The 'normal' may need to be 'side' instead.

  25. Kasper Isager
    Member
    Posted 2 years ago #

    Works like a charm now! Thanks a bunch.
    Do you perhaps know of a way to hide the "Characters" category (In the "Select category" metabox when creating normal posts) from all users as well as hide it the "Categories" menu item in the CPT?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags