Make WordPress Core

Opened 8 years ago

Closed 3 years ago

Last modified 3 years ago

#5096 closed defect (bug) (duplicate)

wp_set_post_categories fails if input array index starts with other than 0

Reported by: borgar Owned by: ryan
Milestone: Priority: normal
Severity: normal Version: 2.3
Component: Taxonomy Keywords: has-patch
Focuses: Cc:


wp_set_post_categories is defected in that if it is passed a an array of categories who's index starts at other than 0, it returns without any action. I noticed this when tracking another issue in Textpattern import.


$cats = array();
$cats[1] = 10;
wp_set_post_categories($postID, $cats);

This fails because (1==count($cats) && ''==$cats[ 0 ]) == true.

Further, I feel that inconsistent checking behavior needs addressing. Why should this function return true in the single case where it gets an array of values where [0]=='' ?

My suggestion is to use this:

if (!is_array($post_categories) || '' == implode($post_categories))
  $post_categories = array(get_option('default_category'));

Attachments (2)

5096.diff (527 bytes) - added by wojtek.szkutnik 5 years ago.
update-wp-set-post-catgories.diff (1.5 KB) - added by wonderboymusic 3 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 @foolswisdom8 years ago

  • Milestone set to 2.4

comment:2 @ryan8 years ago

It was probably done to work around some bug in the dark past. We can probably get rid of it, but a more conservative fix would be to add an isset on $post_categories[0] to that else if so that you don't get caught by the early return.

comment:3 @Denis-de-Bernardy6 years ago

  • Component changed from General to Taxonomy
  • Owner changed from anonymous to ryan

comment:4 @ryan6 years ago

  • Milestone changed from 2.9 to Future Release

comment:5 @kevinB5 years ago

  • Cc kevinB added

comment:6 @wojtek.szkutnik5 years ago

  • Cc wojtek.szkutnik@… added
  • Keywords gsoc added

@wojtek.szkutnik5 years ago

comment:8 @wojtek.szkutnik5 years ago

  • Keywords has-patch added

comment:9 @wonderboymusic3 years ago

  • Keywords gsoc removed

This now works no prob - patch added:

wp_set_post_categories( 1, array( '', 9 ) );

comment:10 @wonderboymusic3 years ago

  • Milestone Future Release deleted
  • Resolution set to worksforme
  • Status changed from new to closed

This works now in core:

php -r "require( 'wp-load.php' );  
wp_set_post_categories( 1, array( '', '', '', '', '', '', 7, 8, 9 ) ); "

comment:11 @SergeyBiryukov3 years ago

  • Resolution changed from worksforme to duplicate

Duplicate of #13246.

Note: See TracTickets for help on using tickets.