Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#43752 new defect (bug)

ID, post_parent, menu_order on global $post object is a string in edit context; expecting int

Reported by: javorszky's profile javorszky Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.5
Component: Posts, Post Types Keywords: needs-patch needs-unit-tests
Focuses: Cc:


When I'm on an edit post screen, the ID, post_parent, menu_order attributes on the global $post object are strings. I expect them to be integers.

To quickly check, put this in a plugin:

add_action( 'add_meta_boxes', function(){
        add_meta_box( 'foo', 'bar', function( $post ) {

Here's what's happening:

  1. in wp-admin/post.php the edit case happens, and within that the post gets reloaded here:
  2. that function will run the post object through its own filter with filter edit here:
  3. because at the time $this->filter = "raw", and the $filter is edit, that will run the object through sanitize_post here
  4. sanitize_post will, in turn, run all the fields through sanitize_post_field here:
  5. and even though we have 3 fields set as int (, by the time we get to this part (, those three will be ran through esc_attr
  6. esc_attr will feed it through _wp_specialchars here
  7. which begins with $string = (string) $string; here

The part that throws me off is that sanitize_post_field declares these three properties to be integers at the beginning of the function, so I sort of expected them to come out as integers on the other end.

Change History (5)

#1 @javorszky
6 years ago

Related WooCommerce issue on (tldr: it being a string causes it to slow down due to WC expecting the prop to be an int)

#2 follow-up: @javorszky
6 years ago

Original props goes to @alexrsoap for bumping into this and doing debugging that uncovered this.

#3 in reply to: ↑ 2 @alexrsoap
6 years ago

Replying to javorszky:

Original props goes to @alexrsoap for bumping into this and doing debugging that uncovered this.

Thanks for the props. Thanks to you for the legwork of verification and reporting.

This ticket was mentioned in Slack in #core by javorszky. View the logs.

6 years ago

#5 @SergeyBiryukov
6 years ago

  • Keywords needs-patch needs-unit-tests added
Note: See TracTickets for help on using tickets.