Make WordPress Core


Ignore:
Timestamp:
09/04/2013 05:41:03 PM (12 years ago)
Author:
wonderboymusic
Message:

Allow int to be passed in lieu of array, add append arg to wp_set_post_categories(). Adds more extensive unit tests for wp_set_post_categories().

Props ptahdunbar for initial patch.
Fixes #16550.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r25182 r25234  
    32293229 *
    32303230 * @param int $post_ID Post ID.
    3231  * @param array $post_categories Optional. List of categories.
     3231 * @param array|int $post_categories Optional. List of categories or ID of category.
     3232 * @param bool $append If true, don't delete existing categories, just add on. If false, replace the categories with the new categories.
    32323233 * @return bool|mixed
    32333234 */
    3234 function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
     3235function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) {
    32353236    $post_ID = (int) $post_ID;
    32363237    $post_type = get_post_type( $post_ID );
    32373238    $post_status = get_post_status( $post_ID );
    32383239    // If $post_categories isn't already an array, make it one:
    3239     if ( !is_array($post_categories) || empty($post_categories) ) {
    3240         if ( 'post' == $post_type && 'auto-draft' != $post_status )
     3240    $post_categories = (array) $post_categories;
     3241    if ( empty( $post_categories ) ) {
     3242        if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
    32413243            $post_categories = array( get_option('default_category') );
    3242         else
     3244            $append = false;
     3245        } else {
    32433246            $post_categories = array();
     3247        }
    32443248    } else if ( 1 == count($post_categories) && '' == reset($post_categories) ) {
    32453249        return true;
    32463250    }
    32473251
    3248     return wp_set_post_terms($post_ID, $post_categories, 'category');
     3252    return wp_set_post_terms( $post_ID, $post_categories, 'category', $append );
    32493253}
    32503254
Note: See TracChangeset for help on using the changeset viewer.