How to Add Category Selection to Custom Post Type in WordPress

Home » Snippets » How to Add Category Selection to Custom Post Type in WordPress
0

Created with:

Visibility: 

public

Creator: Alex

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re building a custom post type in WordPress, you might want to add the ability to categorize your posts. By default, WordPress supports categories and tags for regular post types like posts and pages, but if you’re creating a custom post type, you’ll need to add this functionality yourself. In this article, we’ll show you how to easily add category selection to your custom post type using just a few lines of code.

					function WPTurbo_add_category_selection_to_custom_post_type() {
    $args = array(
      'public'   => true,
      '_builtin' => false
    );
    $output = 'names'; // or objects
    $operator = 'and'; // 'and' or 'or'
     
    $taxonomies = get_taxonomies( $args, $output, $operator ); 
     
    foreach ( $taxonomies as $taxonomy ) {
        add_meta_box(
            'wpturbo_category_selection',
            __( 'Category Selection', 'wpturbo' ),
            'WPTurbo_category_selection_callback',
            $taxonomy,
            'side',
            'default'
        );
    }
}
add_action( 'add_meta_boxes', 'WPTurbo_add_category_selection_to_custom_post_type' );

function WPTurbo_category_selection_callback( $post ) {
    $taxonomy = $post->taxonomy;
    $terms = get_terms( $taxonomy );
    ?>
    <div class="categorydiv">
        <div class="tabs-panel">
            <ul id="<?php echo $taxonomy; ?>checklist" class="categorychecklist form-no-clear">
                <?php foreach ( $terms as $term ) : ?>
                    <li>
                        <label class="selectit">
                            <input
                                type="checkbox"
                                id="in-<?php echo $taxonomy; ?>-<?php echo $term->term_id; ?>"
                                name="<?php echo $taxonomy; ?>[]"
                                value="<?php echo $term->term_id; ?>"
                                <?php checked( in_array( $term->term_id, wp_get_post_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ) ), true ); ?>
                            />
                            <?php echo $term->name; ?> (<?php echo $term->count; ?>)
                        </label>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>
    <?php
}
				

This code snippet demonstrates how to add a category selection meta box to a custom post type in WordPress. The WPTurbo_add_category_selection_to_custom_post_type() function is used to define the taxonomy options that will be displayed in the meta box.

The $args array is used to define which taxonomies to include in the meta box. In this example, we are retrieving all custom taxonomies using the get_taxonomies() function. The $output variable is set to 'names' to return only the taxonomy names (as opposed to full objects), and the $operator variable is set to 'and' to return a list of taxonomies that meet all the criteria set in $args.

In the foreach loop, we use the add_meta_box() function to create a meta box for each taxonomy returned in the $taxonomies array. Inside the add_meta_box() function, we specify the title, callback function, taxonomy, and location for each meta box.

Next, we define the WPTurbo_category_selection_callback() function, which is the callback function specified in the add_meta_box() function. This function is responsible for rendering the actual category selection options in the meta box.

We use the $post variable to retrieve the current taxonomy and define an array of terms using the get_terms() function. Inside the HTML markup, we use a foreach loop to iterate over each term and display a checkbox input for it.

The checked() function is used to determine whether each checkbox should be marked as checked based on the terms associated with the current post. If a term is associated with the post, the checked() function will return true, which will cause the checkbox to be checked.

By adding this code snippet to your custom post type's functions file, your users can easily assign categories to their custom posts right from the post editor screen.

Register an account to save your snippets or go Pro to get more features.