• 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

Viewing 9 replies - 16 through 24 (of 24 total)
  • Hey Ian, we both were at SCAD at the same time!

    Ian

    (@ianaleksander)

    Small World 😀

    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!

    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.

    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?

    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');
    
    ?>

    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 -.-

    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.

    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?

Viewing 9 replies - 16 through 24 (of 24 total)
  • The topic ‘Set category to a custom post type automatically’ is closed to new replies.