WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 16 months ago

Last modified 16 months 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:

Description

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.

Example:

$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 4 years ago.
update-wp-set-post-catgories.diff (1.5 KB) - added by wonderboymusic 20 months ago.

Download all attachments as: .zip

Change History (13)

comment:1 foolswisdom7 years ago

  • Milestone set to 2.4

comment:2 ryan7 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-Bernardy5 years ago

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

comment:4 ryan4 years ago

  • Milestone changed from 2.9 to Future Release

comment:5 kevinB4 years ago

  • Cc kevinB added

comment:6 wojtek.szkutnik4 years ago

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

wojtek.szkutnik4 years ago

comment:8 wojtek.szkutnik4 years ago

  • Keywords has-patch added

comment:9 wonderboymusic20 months ago

  • Keywords gsoc removed

This now works no prob - patch added:

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

comment:10 wonderboymusic16 months 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 SergeyBiryukov16 months ago

  • Resolution changed from worksforme to duplicate

Duplicate of #13246.

Note: See TracTickets for help on using tickets.