1

I've created two new CPTs called 'Movie Reviews' and "Game Reviews". In my website I also have one category called 'Reviews' where you can find two sub-categories: 'Movie Reviews' and 'Game Reviews'.

I saw that I can set a specific taxonomy for each CPT, but what I want is to automaticly set one specific category to every post in this CPT. For example, if I write a movie review in my CPT 'Movie Reviews', I want to automaticly set the category of this post as 'Reviews' - 'Movie Reviews', because every post under this CPT will be like that.

Is this possible? How should I proceed?

  • 2
    This question has been answered in wordpress.stackexchange.com/questions/49369/… – ville6000 Nov 12 '15 at 14:17
  • I think you should proceed with going to the link and testing with some code. If you run into an issue you should come back and make an edit with code and then we could help. If you are wanting someone to do it for you I dont think that's going to happen. As ville pointed this has been asked before. – DᴀʀᴛʜVᴀᴅᴇʀ Nov 12 '15 at 14:23
  • I didn't see that question before, thank you @ville6000! I'll try it out now :) Anyway, I didn't know how should I start working with it and that's why I tried to ask for some help. Your rudeness isn't helping, @Darth_Vader. – Neks Nov 12 '15 at 14:33
  • I'm not trying to be rude. Just being honest. If you don't know where to start you should explain that and at least try to show us something. I think its rude when people expect people that have spent countless time and effort to learn something wanting answers when they don't even try. – DᴀʀᴛʜVᴀᴅᴇʀ Nov 12 '15 at 14:48
  • You're assuming I'm not trying anything while I told you I tried with the taxonomy but it's not giving me what I want. I also spent countless hours trying to learn, but I'm pretty new into this. I'm not expecting you to do my job. If you read, you'll see I'm just asking for a route, as the newbie I am :) This community is supposed to be a place to share knowledge, and what you said isn't helping me to achieve nothing, just pissing me off. I'll quit the offtopic now. Thanks anyway. – Neks Nov 12 '15 at 15:23
0

You will need to run an action to check post type every time a new post is being created.

Just add this code in functions.php in your active theme folder located in "wp-content/themes"

function post_auto_cat( $post_ID ) {
    $post_type = 'Your custom post type. For example: movie';
    $cat_id = 123; // Your reviews category id (for example: 123)
    $post_categories=array($cat_id);

    // check if current post type is movie review
    if(get_post_type($post_ID)==$post_type) {
        // assign a category for this post by default
        wp_set_post_categories( $post_ID, $post_categories );
    }

   return $post_ID;
}
add_action( 'publish_post', 'post_auto_cat' );

It will assign the category for each post with the custom post type 'movie' to the category reviews with the ID 123 - EVERY time a new post gets published.

If you wanted to make this check fires every time a post is being updated (not just published) you will have to change the last line from

add_action( 'publish_post', 'post_auto_cat' );

to

add_action( 'save_post', 'post_auto_cat' );

Remember to change the values in the above mentioned code to your values (post type & category id)

This code looks legit but i didn't test so remember to backup your files/database before using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.