WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 20 months ago

#16550 closed enhancement (fixed)

wp_set_post_categories should take an integer

Reported by: ptahdunbar Owned by: wonderboymusic
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.0
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description

wp_set_post_categories(); can only be passed an array of cat ids. If you pass it an integer, it resets the value to the default category even though the category exists.

This happens as the if statement runs an OR check to see if the second parameter is not an array or empty. If one is true, it just goes on ahead and resets the parameter to be the default category regardless if the value was empty or a string.

Patch fixes this by strict typing the second parameter to an array. It also strict types the default category id as it was being returned as a string.

Attachments (4)

ticket.16550.diff (626 bytes) - added by ptahdunbar 4 years ago.
ticket.16550.2.diff (662 bytes) - added by ptahdunbar 4 years ago.
16550.diff (675 bytes) - added by wonderboymusic 20 months ago.
16550.2.diff (3.5 KB) - added by wonderboymusic 20 months ago.

Download all attachments as: .zip

Change History (10)

@ptahdunbar4 years ago

comment:1 follow-up: @ocean904 years ago

  • Component changed from General to Taxonomy

Please provide a patch relative to trunk.

@ptahdunbar4 years ago

comment:2 in reply to: ↑ 1 @ptahdunbar4 years ago

Replying to ocean90:

Please provide a patch relative to trunk.

Oops. There ya go.

comment:3 @solarissmoke4 years ago

  • Keywords has-patch added

comment:4 @nacin4 years ago

  • Milestone changed from Awaiting Review to Future Release
  • Summary changed from wp_set_post_categories is defected to wp_set_post_categories should take an integer
  • Type changed from defect (bug) to enhancement
  • Version changed from 3.1 to 3.0

Re-classifying this ticket. It's not a defect -- wp_set_post_categories() explicitly takes an array, so array( 3 ) rather than 3.

Other functions such as wp_set_object_terms() seem to not mind taking a scalar argument. So this looks like a fine enhancement.

@wonderboymusic20 months ago

comment:5 @wonderboymusic20 months ago

  • Milestone changed from Future Release to 3.7

@wonderboymusic20 months ago

comment:6 @wonderboymusic20 months ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 25234:

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.

Note: See TracTickets for help on using tickets.