Improve the include / exclude SQL generation in `get_pages()` by using `IN` and `NOT IN` where applicable. Adds unit tests for include / exclude.

Fixes #22074.


Built from https://develop.svn.wordpress.org/trunk@25168


git-svn-id: http://core.svn.wordpress.org/trunk@25144 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2013-08-29 18:49:09 +00:00
parent 29952baa41
commit 0624fe960b
1 changed files with 6 additions and 22 deletions

View File

@ -3671,7 +3671,7 @@ function get_pages($args = '') {
$cache = array();
$inclusions = '';
if ( !empty($include) ) {
if ( ! empty( $include ) ) {
$child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
$parent = -1;
$exclude = '';
@ -3679,32 +3679,16 @@ function get_pages($args = '') {
$meta_value = '';
$hierarchical = false;
$incpages = wp_parse_id_list( $include );
if ( ! empty( $incpages ) ) {
foreach ( $incpages as $incpage ) {
if (empty($inclusions))
$inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage);
else
$inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage);
}
}
if ( ! empty( $incpages ) )
$inclusions = ' AND ID IN (' . implode( ',', array_map( 'intval', $incpages ) ) . ')';
}
if (!empty($inclusions))
$inclusions .= ')';
$exclusions = '';
if ( !empty($exclude) ) {
if ( ! empty( $exclude ) ) {
$expages = wp_parse_id_list( $exclude );
if ( ! empty( $expages ) ) {
foreach ( $expages as $expage ) {
if (empty($exclusions))
$exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage);
else
$exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage);
}
}
if ( ! empty( $expages ) )
$exclusions = ' AND ID NOT IN (' . implode( ',', array_map( 'intval', $expages ) ) . ')';
}
if (!empty($exclusions))
$exclusions .= ')';
$author_query = '';
if (!empty($authors)) {