From 0624fe960b3841507a8bb5783c01c96cfd9fdcde Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 29 Aug 2013 18:49:09 +0000 Subject: [PATCH] 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 --- wp-includes/post.php | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index c306d67baf..25606cd4e1 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -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)) {