How to Pre-Select a Category for a New Post

Lets say you have a category called “X”, and you want to create a link which when clicked will take the user to the “Add New Post” screen with the “X” category already selected. This could be used to simplify posting for non-technical users, or even as a way to implement “poor-man’s custom post types” of sorts, with categories instead of true CPTs.

To make this possible, you’ll need a way to set the post category via the URL. The following snippet lets you do just that. Simply append a category_id=123 parameter to the post editor URL, and it will automatically select the specified category in the “Categories” widget.

Place this code in your functions.php or a functionality plugin:

function ws_preselect_post_category() {
	if ( isset($_GET['category_id']) && is_numeric($_GET['category_id']) ) {
		$catId = intval($_GET['category_id']);
		?>
		<script type="text/javascript">
			jQuery(function() {
				var catId = <?php echo json_encode($catId); ?>;
				jQuery('#in-category-' + catId).click();
			});
		</script>
		<?php
	}
}
add_action('admin_footer-post-new.php', 'ws_preselect_post_category');

Now you can pass in the category ID like this: /wp-admin/post-new.php?category_id=123.

For example, suppose you have a web development blog that has a “JavaScript” category with the ID = 15. You could use this snippet to add a Posts -> Add New JS Article link to the admin menu. When you click it, the “JavaScript” category will be selected automatically:

function ws_add_new_post_link() {
	add_posts_page(
		'Add New JS Article',
		'Add New JS Article',
		'edit_posts',
		'post-new.php?category_id=15'
	);
}
add_action('admin_menu', 'ws_add_new_post_link');
Related posts :

16 Responses to “How to Pre-Select a Category for a New Post”

  1. upto what version of wp will this work?

  2. Jānis Elsts says:

    I just tested it in WordPress 4.4-alpha. Still works.

  3. Hello, l have seen your work and is of high quality, i would like to ask how to depict the categories into pages so to be better for SEO?

    Maybe l should use a shortcode?

  4. junvin says:

    thank you so much….great function!!!

  5. ITilam says:

    thanks for sharing keep it up

  6. J. says:

    We had some trouble figuring out the admin_footer-post-new.php piece. Was this a part of older WordPress versions?

  7. Jānis Elsts says:

    I haven’t tried it in a while, but WordPress docs say that it’s still valid:
    https://codex.wordpress.org/Plugin_API/Action_Reference/admin_footer-(hookname)

  8. Komal says:

    I am new to js and learning js. Can we use this in a wordpress website?

  9. Adrian says:

    Hi

    Is there a way to create a new menu section like

    JavaSript
    All JS Articles
    Add New JS Article <- to do what the example above does

    but without having to put these menu options under the Post menu option? I am trying to do something like the blog above (for various reasons I do not want to use custom post types) but I want a separate menu item with sub-items.

    Thank you

  10. Jānis Elsts says:

    Take a look at the WordPress API documentation on adding new admin menus. There are ways to add menu items to different locations.

  11. Adrian says:

    Thanks

    I got it working. Your Pre-Select category code is so neat 🙂

  12. Jody Mitoma says:

    I just have to take a moment and say THANK YOU for this hack. Still working in JULY 2019!

    I’ve been trying to get this working on my missing persons website, and wish I had implemented this years ago. Every little bit saves time, especially when you’re publishing a ton of content. Cheers!! ^_^

  13. I nver knew this befor. Can’t wait give a try. Hope it works. Thanks!

  14. is this still working? I tested it in my site but it does not work.

  15. Jānis Elsts says:

    I think it should still work, but only if you’re using the classic post editor. It probably won’t work at all with the new block editor, Gutenberg. That editor may require a completely different approach.

  16. […] How to Pre-Select a Category for a New Post […]

Leave a Reply