Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to have a link to create a new post that sets the category also.

I have tried wp-admin/post-new.php?post_category=12 and wp-admin/post-new.php?cat=12, but neither worked. I also tried using the name rather than the id of the category; which also had no affect.

How do I create a link to a new post with a default category?

share|improve this question

2 Answers 2

Hook into wp_insert_post, test the post status for auto-draft, and the URL for a GET parameter.

But first we need a helper function to get and sanitize the GET parameter:

 * Set default category.
 * @wp-hook pre_option_default_category
 * @return  string Category slug
function t5_get_default_cat_by_url()
    if ( ! isset( $_GET['post_cat'] ) )
        return FALSE;

    return array_map( 'sanitize_title', explode( ',', $_GET['post_cat'] ) );

Now the auto-draft handler:

add_action( 'wp_insert_post', 't5_draft_category', 10, 2 );

 * Add category by URL parameter to auto-drafts.
 * @wp-hook wp_insert_post
 * @param   int $post_ID
 * @param   object $post
 * @return  WP_Error|array An error object or term ID array.
function t5_draft_category( $post_ID, $post )
    if ( ! $cat = t5_get_default_cat_by_url()
        or 'auto-draft' !== $post->post_status )

    // return value will be used in unit tests only.
    return wp_set_object_terms( $post_ID, $cat, 'category' );

This works only if get_default_post_to_edit() was called with the second parameter $create_in_db set to TRUE. To catch the other case you have to filter the option default_category:

add_filter( 'pre_option_default_category', 't5_get_default_cat_by_url' );

Now you can use the parameter post_cat to pass a comma separated list of category slugs:

enter image description here

See also:

share|improve this answer
I tried this solution by pasting the code into my functions.php file and it didn't work. Am I missing something? Do I need to do more like call the function to make it do the job? –  Jamie Jan 16 at 0:40
@Jamie Didn't work is too vague. :) –  toscho Jan 16 at 0:42
I thought by adding the code into functions.php that when I tick a category, it would add that parameter to the end of the string like in the photo above. I am trying to add a metabox to only a specific category. Then I will use $_GET to test if I am in the correct category to load the metabox. When I tried the code and tried to replicate what is in your photo above, nothing happened. All I got was wp-admin/post-new.php. I am trying to get wp-admin/post-new.php?post_cat=audio –  Jamie Jan 16 at 0:50
@Jamie This code works the other way around: You build the link manually (for example to send it to someone per email), and the code will set the proper category. It doesn’t create any link. –  toscho Jan 16 at 0:53

I think you can go about the default option default_category and filter option_default_category this, if the url have a param for the category, like this example source. Use it as plugin, test it. Was write from scratch and not tested.

The url param is post_cat and you can set the category, like this url: /wp-admin/post-new.php?post_cat=categoryname

 * Plugin Name: .my Test
 * Plugin URI:  http://bueltge.de/
 * Description: 
 * Version:     0.0.1
 * Author:      Frank B&uuml;ltge
 * Author URI:  http://bueltge.de/
class Set_Default_Cat_From_Url_Param {

    protected static $classobj = NULL;

    public static function init() {

        NULL === self::$classobj and self::$classobj = new self();

        return self::$classobj;

    function __construct() {

        if ( isset( $_GET['post_cat'] ) )
            add_filter( 'option_default_category', array( $this, 'get_category' ) );

    function get_category( $category ) {

        if ( isset( $_GET['post_cat'] ) )
            $category = get_cat_ID( esc_attr( $_GET['post_cat'] ) );

        return $category;

add_action( 'load-post-new.php', array( 'Set_Default_Cat_From_Url_Param', 'init' ) );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.