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. git-svn-id: https://develop.svn.wordpress.org/trunk@25168 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
dd78079286
commit
f458d16c01
@ -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 ) ) {
|
||||
$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)) {
|
||||
|
@ -798,4 +798,29 @@ class Tests_Post extends WP_UnitTestCase {
|
||||
_unregister_post_type( $post_type );
|
||||
$this->assertEquals( new stdClass, wp_count_posts( $post_type, 'readable' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 22074
|
||||
*/
|
||||
function test_get_pages_include_exclude() {
|
||||
$page_ids = array();
|
||||
|
||||
foreach ( range( 1, 20 ) as $i )
|
||||
$page_ids[] = $this->factory->post->create( array( 'post_type' => 'page' ) );
|
||||
|
||||
$inc = array_slice( $page_ids, 0, 10 );
|
||||
sort( $inc );
|
||||
$exc = array_slice( $page_ids, 10 );
|
||||
sort( $exc );
|
||||
|
||||
$include = get_pages( array( 'include' => $inc ) );
|
||||
$inc_result = wp_list_pluck( $include, 'ID' );
|
||||
sort( $inc_result );
|
||||
$this->assertEquals( $inc, $inc_result );
|
||||
|
||||
$exclude = get_pages( array( 'exclude' => $exc ) );
|
||||
$exc_result = wp_list_pluck( $exclude, 'ID' );
|
||||
sort( $exc_result );
|
||||
$this->assertEquals( $inc, $exc_result );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user