WordPress.org

Make WordPress Core

Opened 6 days ago

Last modified 6 days ago

#50506 new enhancement

PHP warning appearing in 404 page and 'post_type' query var is Array

Reported by: arl1nd Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.4.2
Component: Canonical Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description

Fixed PHP warning appearing in 404 page and 'post_type' field is Array (not string), more exactly function: redirect_guess_404_permalink line 700 in wp-includes/canonical.php

To replicate this issue simply add this code inside plugin/theme:

function _debug_php_warning_on_404( $query ) {
	$query->set( 'post_type', [ 'post', 'portfolio', 'page' ] );
} 
add_action( 'pre_get_posts', '_debug_php_warning_on_404' );

Then try a missing page slug domain.test/missing-content it will show a warning:

wpdb::prepare was called incorrectly. The query only expected one placeholder, but an array of multiple placeholders was sent. Please see Debugging in WordPress for more information. (This message was added in version 4.9.0.)

The fix is to replace line 700 and check for array "post_type" query var:

if ( $post_type = get_query_var( 'post_type' ) ) {
	if ( is_array( $post_type ) ) {
		$where .= " AND post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')";
	} else {
		$where .= $wpdb->prepare( ' AND post_type = %s', $post_type );
	}
}

Attachments (1)

canonical.php (30.6 KB) - added by arl1nd 6 days ago.
Fixed version of code

Download all attachments as: .zip

Change History (2)

@arl1nd
6 days ago

Fixed version of code

#1 @SergeyBiryukov
6 days ago

  • Component changed from General to Canonical
  • Keywords needs-patch needs-unit-tests added
Note: See TracTickets for help on using tickets.