Archives

WordPress: Showing subcategories for edit posts

Hosting-General Topics,HTML and Web Building

If you create a post, save it in a subcategory (instead of a top level category), and you later want to change the location of that post to another subcategory, you may find that the other subcategories that you could move it to have vanished, and only the top level categories are visible.

For example, let’s say you were writing an article on personal loan debt consolidation (where you join multiple loans together into one combined loan – to make it easier to pay and often at a lower overall interest rate). You write the article and when you create it in WordPress admin, set the category to “Debt Consolidation” which you have set up as a subcategory of “Personal Loans”.

If you later decide to go back and edit that article to include information about car finance, when you get to the edit post screen in admin, the “car finance” subcategory is gone. Where did it go?

This is caused by the default behaviour of WordPress which puts the chosen category at the top of the categories box in Edit Post. The code that checks for the checked category and pushes it up to the top of the list then loses the connection between the subcategory and the parent.

As at the time of writing (WordPress 3.0.5), the only easy way to resolve this issue is to edit some core WordPress code. It’s not a hard edit – it involves “turning off” the feature that pushes the checked categories to the top of the list.

Here’s how:

Open the file wp-admin/includes/template.php

Find the line function wp_terms_checklist in the code. The code around it will look a bit like this:

/**
 * Taxonomy independent version of wp_category_checklist
 *
 * @param int $post_id
 * @param array $args
 */
function wp_terms_checklist($post_id = 0, $args = array()) {

Scroll down in this function and find a piece of code towards the end of this function that looks like this:

		foreach( $keys as $k ) {
			if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
				$checked_categories[] = $categories[$k];
				unset( $categories[$k] );
			}
		}

Comment this out by putting /* in front of this block of code, and */ at the end of it. So it will look like this:

	/*	foreach( $keys as $k ) {
			if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
				$checked_categories[] = $categories[$k];
				unset( $categories[$k] );
			}
		} */

And you’re done. The Edit Post categories box now looks the same as the Create Posts categories box, and you can add “Car Finance” as a category for your post.