WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#17014 closed defect (bug) (duplicate)

Passing post_status an array for a new WP_Query throws an error

Reported by: drebabels Owned by:
Milestone: Priority: normal
Severity: minor Version:
Component: Query Keywords:
Focuses: Cc:

Description

According to the WP_Query documentation (http://codex.wordpress.org/Function_Reference/WP_Query#Type_.26_Status_Parameters) the post_status parameter takes either an array or string. However when you pass it an array like so

$args = array (
	'post_status' => array('inherit','publish')
);
$query = new WP_Query( $args );

The following two errors are thrown:

Warning: explode() expects parameter 2 to be string, array given in /Users/ender/Sites/soompi3k_trunk/htdocs/wp-includes/query.php on line 2369

Warning: in_array() expects parameter 2 to be array, null given in /Users/ender/Sites/soompi3k_trunk/htdocs/wp-includes/query.php on line 2378

And the SQL query looks like this:

SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' ORDER BY wp_posts.post_date DESC LIMIT 0, 10

But pass in a comma separated string like so

$args = array (
	'post_status' => 'inherit, publish'
);
$query = new WP_Query( $args );

No errors are thrown and the resulting SQL query is what I would expect:

SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'inherit') ORDER BY wp_posts.post_date DESC LIMIT 0, 10

Change History (2)

comment:1 @sivel4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version 3.1 deleted

Just as a precautionary mention, the Codex is not official documentation and cannot always be trusted. The Codex is community supplied documentation and has been known to often contain incorrect information.

In any case, this is a duplicate of a ticket that already has a patch. Closing as such. See #16824

comment:2 @hakre4 years ago

Codex page now has a warning.

Note: See TracTickets for help on using tickets.