WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to set additional "default" categories. I figured by hooking into the wp_insert_post_data filter, I could use $postarr to set the category. It doesn't seem to be working? Can someone offer advice as to how I would go about doing this?

add_filter( 'wp_insert_post_data', 's3x9s_wp_insert_post_data', '99', 2 );
function s3x9s_wp_insert_post_data( $data, $postarr ) {
    array_push( $postarr['post_category'], 123 );
    return $data;
share|improve this question
up vote 0 down vote accepted

I think that the best way to set the default category is in the wp-admin: Settings > Writings > Default post Category However, I think you're looking for setting more than one category by default.

If you look the documentation of wp_insert_post_data, there isn't any key such as post_category.

Perhaps, you should use publish_post

Runs when a post is published, or if it is edited and its status is "published". Action function arguments: post ID.

There, you should use wp_set_post_categories

So, the code would be something similar to this:

function set_category_by_default( $post_ID ) {
    wp_set_post_categories( $post_ID, array(123, 124) ) ;
add_action( 'publish_post', 'set_category_by_default', 5, 1 );
share|improve this answer
should the function check that no category has been specified for the said post before setting the 'default' categories? – Gregory Dec 18 '12 at 10:56

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.