Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 696263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:02:43+00:00 2026-05-14T03:02:43+00:00

The following code will add the categories selector widget to the WordPress Page editor

  • 0

The following code will add the categories selector widget to the WordPress Page editor interface…

add_action('admin_menu', 'my_post_categories_meta_box');
function my_post_categories_meta_box() {
    add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'core');
}

What I would like to do is to figure out how to modify the resulting category listing so that it only contains a predefined list of hard coded categories that I define.

Since I’m adding this via my custom theme, it will only appear on the page editor when my theme is active on the site. And I have some specific “handler” categories that my theme installs into the site and later uses to determine layout elements. I only want these specific categories to be listed in this particular instance of the categories widget.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:02:43+00:00Added an answer on May 14, 2026 at 3:02 am

    Use a modified version of post_categories_meta_box, in which you change the call to wp_category_checklist to a modified version, wp_category_checklist_modified.

    post_categories_meta_box_modified:

    function post_categories_meta_box_modified($post) {
    ?>
    <ul id="category-tabs">
        <li class="ui-tabs-selected"><a href="#categories-all" tabindex="3"><?php _e( 'All Categories' ); ?></a></li>
        <li class="hide-if-no-js"><a href="#categories-pop" tabindex="3"><?php _e( 'Most Used' ); ?></a></li>
    </ul>
    
    <div id="categories-pop" class="ui-tabs-panel" style="display: none;">
        <ul id="categorychecklist-pop" class="categorychecklist form-no-clear" >
            <?php $popular_ids = wp_popular_terms_checklist('category'); ?>
        </ul>
    </div>
    
    <div id="categories-all" class="ui-tabs-panel">
        <ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
            <?php wp_category_checklist($post->ID, false, array($cat1_id, $cat2_id.... ,$catn_id), $popular_ids) ?>
        </ul>
    </div>
    
    <?php if ( current_user_can('manage_categories') ) : ?>
    <div id="category-adder" class="wp-hidden-children">
        <h4><a id="category-add-toggle" href="#category-add" class="hide-if-no-js" tabindex="3"><?php _e( '+ Add New Category' ); ?></a></h4>
        <p id="category-add" class="wp-hidden-child">
            <label class="hidden" for="newcat"><?php _e( 'Add New Category' ); ?></label><input type="text" name="newcat" id="newcat" class="form-required form-input-tip" value="<?php _e( 'New category name' ); ?>" tabindex="3" aria-required="true"/>
            <label class="hidden" for="newcat_parent"><?php _e('Parent category'); ?>:</label><?php wp_dropdown_categories( array( 'hide_empty' => 0, 'name' => 'newcat_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent category'), 'tab_index' => 3 ) ); ?>
            <input type="button" id="category-add-sumbit" class="add:categorychecklist:category-add button" value="<?php _e( 'Add' ); ?>" tabindex="3" />
            <?php wp_nonce_field( 'add-category', '_ajax_nonce', false ); ?>
            <span id="category-ajax-response"></span>
        </p>
    </div>
    <?php
    endif;
    
    }
    

    I’ve only change the line of the original function

    <?php wp_category_checklist($post->ID, false, false, $popular_ids) ?>
    

    to

    <?php wp_category_checklist_modified($post->ID, false, false, $popular_ids) ?>
    

    wp_category_checklist_modified:

    function wp_category_checklist_modified( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $include_cats = array() ) {
        $walker = new Walker_Category_Checklist;
        $descendants_and_self = (int) $descendants_and_self;
        $cat_ids_list = implode(',', $include_cats);
    
        $args = array();
    
        if ( is_array( $selected_cats ) )
            $args['selected_cats'] = $selected_cats;
        elseif ( $post_id )
            $args['selected_cats'] = wp_get_post_categories($post_id);
        else
            $args['selected_cats'] = array();
    
        if ( is_array( $popular_cats ) )
            $args['popular_cats'] = $popular_cats;
        else
            $args['popular_cats'] = get_terms( 'category', array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
    
        if ( $descendants_and_self ) {
            $categories = get_categories( "child_of=$descendants_and_self&hierarchical=0&hide_empty=0&include=$cat_ids_list" );
            $self = get_category( $descendants_and_self );
            array_unshift( $categories, $self );
        } else {
            $categories = get_categories('get=all&include='. $cat_ids_list);
        }
    
        // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
        $checked_categories = array();
        for ( $i = 0; isset($categories[$i]); $i++ ) {
            if ( in_array($categories[$i]->term_id, $args['selected_cats']) ) {
                $checked_categories[] = $categories[$i];
                unset($categories[$i]);
            }
        }
    
        // Put checked cats on top
        echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
        // Then the rest of them
        echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
    }
    

    Here I’ve added an extra argument to wp_category_checklist_modified, $include_cats, in which you can specify the categories ids, then I use this list in the two calls to get_categories passing it as the include parameter.

    These functions are not documented (as much as I am be able to find), so I’ve had to take a look to the source code.

    Then you will simply use

    function my_post_categories_meta_box() {
       add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box_modified', 'page', 'side', 'core');
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When IE8 is released, will the following code work to add a conditional stylesheet?
The following code will generate a link to the page I want to get
The application will run fine unless I add the following code. public class TFView
The following code will not run correctly in IE7 with the latest service packs
Will the following code result in a deadlock using C# on .NET? class MyClass
I have the following code, which will not work. The javascript gives no errors
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
I found the following code to create a tinyurl.com url: http://tinyurl.com/api-create.php?url=http://myurl.com This will automatically
i have the following jquery code. basically i will have several overlapped divs and
I'm trying to write a piece of code that will do the following: Take

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.