0

I'm trying to connect a custom post type to a taxonomy defined in an other custom post type.

I'll show you an example. I have a custom post type COURSE, i use this post type to create a class for a university, so in this post type i'll have some custom field (teacher, lesson schedule ecc ecc) and a taxonomy called Teacher.

Teacher is an other custom post type i use to represent teachers working in my university.

Now i would like to create a link between the Teacher instance and the Course, so while i'm creating a new course i will have the list of ALL teachers in the university and i can chose one (or more) from this list.

How can i do that, should i define this list of teachers while i'm registering the taxonomy Teacher? Can you give me some code snippet to create this kind of association?

IMPORTANT: i'm looking for a code answer, i don't need a plugin, i want to learn how to create a code to do that.

Thanks in advance.

  • Ok, as you don't want to use a plugin you have two options (that I can think of). 1 - Manually add a term to the teachers taxonomy everytime you creaet a teacher post. Thats's not a nice option :( 2 - Hook the save_post action and programatically create a new term in the teacher taxonomy matching a newly created teacher post. Look here to get started - codex.wordpress.org/Plugin_API/Action_Reference/save_post - have a bash, post your results above as an update and me & others on here will help you going forward. – David Gard Jan 21 '15 at 13:50
  • So, do you have a custom field, a custom post type and a custom taxonomy for Teacher? What instance of Teacher do you want to be connected with Course? – cybmeta Jan 21 '15 at 14:53
0

Ok, so I'm feeling generous and will add another answer :)

This code will run every time a post is created or updated, and it will add a term to your teacher taxonomy with a name that is the same as the post title.

add_action('save_post', 'my_create_teacher_term_from_post');
function my_project_updated_send_email($post_id){

    /** Ensure that this is not a revision */
    if(wp_is_post_revision($post_id))
        return;

    /** Get the name of the teacher that has been added/updated */
    $post_title = get_the_title(post_id);

    /** Check to see if a term with the same name as the teacher exists... */
    if(!term_exists($post_title, 'teacher')) :  // It does not...

        /** Insert the new term */
        wp_insert_term($post_title, 'teacher');

    endif;

}

Note - If you only want this action to occur when a post is created, not updated, replace the save_post hook with wp_insert_post.


Additional reading

  • No problem. Should we ever meet in the pub, I drink any real ale and love dry roasted peanuts ;) – David Gard Jan 21 '15 at 15:44
0

I'd suggest that you take a look at the Posts 2 Posts plugin.

This plugin allows you to create a link between posts of any type. For example -

p2p_register_connection_type(array(
    'name' => 'courses_to_teachers',
    'from' => 'course',
    'to' => 'teacher'
));

You'll then have an additional box when editing a post of either type - https://wordpress.org/plugins/posts-to-posts/screenshots/

So if you are adding/editing a course you can link it to multiple teachers, or vice-versa

I have used this in the past, and while it can be a little complicated to start with, there is very good support and I alwasys found the author very good at answering questions posted via the WP support forum.

The Author, Scribu, has his own website related to the plugin that is full of examples and descriptions - http://scribu.net/wordpress/posts-to-posts/

And there is plenty of support on offer via the WP forums - https://wordpress.org/support/plugin/posts-to-posts

  • Very good but i'm looking for a code solution. I need to learn how to create this associaton using code and not plugin. – Ema.jar Jan 21 '15 at 12:53
  • May I ask why? If a plugin does the job then it means less coding for you :) – David Gard Jan 21 '15 at 12:57

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.