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 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

1 Answer 1

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

 
discard

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.