Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use a modified version of <code>post_categories_meta_box</code>, in which you change the call to <code>wp_category_checklist</code> to a modified version, <code>wp_category_checklist_modified</code>. </p> <p><strong>post_categories_meta_box_modified</strong>:</p> <pre><code>function post_categories_meta_box_modified($post) { ?&gt; &lt;ul id="category-tabs"&gt; &lt;li class="ui-tabs-selected"&gt;&lt;a href="#categories-all" tabindex="3"&gt;&lt;?php _e( 'All Categories' ); ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;li class="hide-if-no-js"&gt;&lt;a href="#categories-pop" tabindex="3"&gt;&lt;?php _e( 'Most Used' ); ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;div id="categories-pop" class="ui-tabs-panel" style="display: none;"&gt; &lt;ul id="categorychecklist-pop" class="categorychecklist form-no-clear" &gt; &lt;?php $popular_ids = wp_popular_terms_checklist('category'); ?&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div id="categories-all" class="ui-tabs-panel"&gt; &lt;ul id="categorychecklist" class="list:category categorychecklist form-no-clear"&gt; &lt;?php wp_category_checklist($post-&gt;ID, false, array($cat1_id, $cat2_id.... ,$catn_id), $popular_ids) ?&gt; &lt;/ul&gt; &lt;/div&gt; &lt;?php if ( current_user_can('manage_categories') ) : ?&gt; &lt;div id="category-adder" class="wp-hidden-children"&gt; &lt;h4&gt;&lt;a id="category-add-toggle" href="#category-add" class="hide-if-no-js" tabindex="3"&gt;&lt;?php _e( '+ Add New Category' ); ?&gt;&lt;/a&gt;&lt;/h4&gt; &lt;p id="category-add" class="wp-hidden-child"&gt; &lt;label class="hidden" for="newcat"&gt;&lt;?php _e( 'Add New Category' ); ?&gt;&lt;/label&gt;&lt;input type="text" name="newcat" id="newcat" class="form-required form-input-tip" value="&lt;?php _e( 'New category name' ); ?&gt;" tabindex="3" aria-required="true"/&gt; &lt;label class="hidden" for="newcat_parent"&gt;&lt;?php _e('Parent category'); ?&gt;:&lt;/label&gt;&lt;?php wp_dropdown_categories( array( 'hide_empty' =&gt; 0, 'name' =&gt; 'newcat_parent', 'orderby' =&gt; 'name', 'hierarchical' =&gt; 1, 'show_option_none' =&gt; __('Parent category'), 'tab_index' =&gt; 3 ) ); ?&gt; &lt;input type="button" id="category-add-sumbit" class="add:categorychecklist:category-add button" value="&lt;?php _e( 'Add' ); ?&gt;" tabindex="3" /&gt; &lt;?php wp_nonce_field( 'add-category', '_ajax_nonce', false ); ?&gt; &lt;span id="category-ajax-response"&gt;&lt;/span&gt; &lt;/p&gt; &lt;/div&gt; &lt;?php endif; } </code></pre> <p>I've only change the line of the original function</p> <pre><code>&lt;?php wp_category_checklist($post-&gt;ID, false, false, $popular_ids) ?&gt; </code></pre> <p>to</p> <pre><code>&lt;?php wp_category_checklist_modified($post-&gt;ID, false, false, $popular_ids) ?&gt; </code></pre> <p><strong>wp_category_checklist_modified:</strong></p> <pre><code>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' =&gt; 'ids', 'orderby' =&gt; 'count', 'order' =&gt; 'DESC', 'number' =&gt; 10, 'hierarchical' =&gt; false ) ); if ( $descendants_and_self ) { $categories = get_categories( "child_of=$descendants_and_self&amp;hierarchical=0&amp;hide_empty=0&amp;include=$cat_ids_list" ); $self = get_category( $descendants_and_self ); array_unshift( $categories, $self ); } else { $categories = get_categories('get=all&amp;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]-&gt;term_id, $args['selected_cats']) ) { $checked_categories[] = $categories[$i]; unset($categories[$i]); } } // Put checked cats on top echo call_user_func_array(array(&amp;$walker, 'walk'), array($checked_categories, 0, $args)); // Then the rest of them echo call_user_func_array(array(&amp;$walker, 'walk'), array($categories, 0, $args)); } </code></pre> <p>Here I've added an extra argument to <code>wp_category_checklist_modified</code>, <code>$include_cats</code>, in which you can specify the categories ids, then I use this list in the two calls to <code>get_categories</code> passing it as the <code>include</code> parameter.</p> <p>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.</p> <p>Then you will simply use</p> <pre><code>function my_post_categories_meta_box() { add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box_modified', 'page', 'side', 'core'); } </code></pre> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload