0

I picked up someone's old project at the company I work at. They created a plugin to add courses to the website (including dates, location etc), but they didn't add the possibility to select a category for the course.

I want to add this function, which I tried by looking at their other code and copy/pasting it and adapting it so it fits my needs.

It does show up on the right side of the page, but whatever I do, it never remembers the category. Category selection showing

I also adapted the "POST" function lower in the file, but I guess I'm still missing a piece of code that actually activates the category-selection, as it is also not showing up in the overview of all courses:

No category displayed

The previous developer worked with add_meta_box to create the boxes on the side. I created the following:

add_meta_box(
        'course_category_box',          // Unique ID 
        esc_html__( 'Category', 'Category Box' ),       // Title
        'render_course_category_box',       // Callback function
        'course',                   // Admin page (or post type)
        'side',                 // Context
        'default'                   // Priority
    );

Which I called upon by using

<?php
    function render_course_category_box( $object, $box ) {

        wp_nonce_field( basename( __FILE__ ), 'course_category_box_nonce' ); ?>
    <p>
         <?php wp_dropdown_categories( $args ); ?> 
    </p>
<?php } ?>

And also added it to the save_course_meta_boxes function.

if ( !isset( $_POST['course_category_box_nonce'] ) || !wp_verify_nonce( $_POST['course_category_box_nonce'], basename( __FILE__ ) ) )
        return $post_id;

Now, I'm probably doing something that is way too complicated, as Wordpress often has a much easier way to add functions like these.

I hope it's clear enough what my problem is. If not, don't hesitate to ask for more info!

3

Alternatively to Kaiser's answer, when you register the post type, simply use the taxonomies attribute:

register_post_type( 'my-cpt', array(
  ...
  'taxonomies' => array( 'category' ),
  ...
) );

Details on register_post_type() can be found here in Codex.

| improve this answer | |
  • Whenever I edit my register_post_type to something similar as you write, it removes quite some functions (disappears from the left menu etc) from the original plugin, because it was using $args before. It also still doesn't save the category. – Rvervuurt Mar 3 '14 at 11:58
  • If you're calling register_post_type( 'course', $args ) then you'll want to set $args['taxonomies'] = array( 'category' ); – Stephen Harris Mar 3 '14 at 12:02
1

When your taxonomy is already registered (or is a built-in one like post tags, categories, etc.), you can simply glue it to a post type by using register_taxonomy_for_object_type():

register_taxonomy_for_object_type( 'category', 'course' );

Just make sure you do that after both the taxonomy and the post type are already registered.

| improve this answer | |
1

Additional info for @StephenHarris answer:

When you register a taxonomy and a post type and want to connect them, there's one important rule:

Always register the taxonomy first. Then register the post type.

The reason is simple: Take a look at the source of register_taxonomy():

// Adds the 2nd argument ($object_type) to the arguments:
$args['object_type'] = array_unique( (array) $object_type );

// ...

// Then the taxonomy gets added the complete Array $args as object
$wp_taxonomies[ $taxonomy ] = (object) $args;

And when you look at the source of register_post_type():

foreach ( $args->taxonomies as $taxonomy ) {
    register_taxonomy_for_object_type( $taxonomy, $post_type );
}

So this calls register_taxonomy_for_object_type() and this will fail if not both the taxonomy is registered globally and the post type is in place. If you're doing it the other way around and register the CPT first, it will simply fail.

| improve this answer | |

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.