Add category selection to custom post type (plugin)
-
07-11-2019 - |
Question
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.
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:
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!
No correct solution