Navigation Menu

Skip to content

Commit

Permalink
Posts, Post Types: Add a new filter for query arguments in get_pages.
Browse files Browse the repository at this point in the history
In [55569] `get_pages` was converted to use `WP_Query` internally. But for plugins that were extending the `get_pages` filters and filter `WP_Query` query arguments, this could result in a conflict. Add a filter `get_pages_query_args` to allow developers to change arguments passed to `WP_Query` but also have the context of the original arguments passed to the `get_pages` function. 

This change also expands test coverage of `get_pages` to ensure no breakages in the future. 

Props spacedmonkey, westonruter, costdev, flixos90, kenwins, marianne38.
See #12821.

git-svn-id: https://develop.svn.wordpress.org/trunk@55845 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
spacedmonkey committed May 22, 2023
1 parent 2748b61 commit 1825c75
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/wp-includes/post.php
Expand Up @@ -6031,7 +6031,7 @@ function get_pages( $args = array() ) {
}
$post_author = $post_author->ID;
}
$query_args['author__in'][] = $post_author;
$query_args['author__in'][] = (int) $post_author;
}
}
}
Expand Down Expand Up @@ -6060,6 +6060,16 @@ function get_pages( $args = array() ) {
$query_args['posts_per_page'] = $number;
}

/**
* Filters query arguments passed to WP_Query in get_pages.
*
* @since 6.3.0
*
* @param array $query_args Array of arguments passed to WP_Query.
* @param array $parsed_args Array of get_pages() arguments.
*/
$query_args = apply_filters( 'get_pages_query_args', $query_args, $parsed_args );

$query = new WP_Query( $query_args );
$pages = $query->get_posts();

Expand Down
183 changes: 183 additions & 0 deletions tests/phpunit/tests/post/getPages.php
Expand Up @@ -325,6 +325,189 @@ public function test_get_pages_include_exclude() {
$this->assertSame( $inc, $exc_result );
}

/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_get_pages_test_filter() {
register_post_type( 'wptests_pt', array( 'hierarchical' => true ) );

$posts = self::factory()->post->create_many(
2,
array(
'post_type' => 'wptests_pt',
)
);
$query_args_values = array();
$parsed_args_values = array();

// Filter the query to return the wptests_pt post type.
add_filter(
'get_pages_query_args',
static function( $query_args, $parsed_args ) use ( &$query_args_values, &$parsed_args_values ) {
$query_args['post_type'] = 'wptests_pt';
$query_args_values = $query_args;
$parsed_args_values = $parsed_args;
return $query_args;
},
10,
2
);

$pages = get_pages();
$page_ids = wp_list_pluck( $pages, 'ID' );
$this->assertSameSets( $posts, $page_ids, 'The return post ids should match the post type wptests_pt.' );

$query_args = array(
'orderby' => array( 'post_title' => 'ASC' ),
'order' => 'ASC',
'post__not_in' => array(),
'meta_key' => '',
'meta_value' => '',
'posts_per_page' => -1,
'offset' => 0,
'post_type' => 'wptests_pt',
'post_status' => array( 'publish' ),
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
);

$this->assertSameSets( $query_args, $query_args_values, 'Query arguments should match expected values' );

$parsed_args = array(
'child_of' => 0,
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => array(),
'include' => array(),
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'parent' => -1,
'exclude_tree' => array(),
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish',
);

$this->assertSameSets( $parsed_args, $parsed_args_values, 'Parsed arguments should match expected values' );
}

/**
* @ticket 12821
* @covers ::get_pages
* @dataProvider data_get_pages_args
*/
public function test_get_pages_args_test_filter( $args, $expected_query_args ) {
$filter = new MockAction();
add_filter( 'get_pages_query_args', array( $filter, 'filter' ), 10, 2 );

$results = get_pages( $args );

$this->assertIsArray( $results, 'get_pages should result an array' );

$filter_args = $filter->get_args();

$default_args = array(
'orderby' => array( 'post_title' => 'ASC' ),
'order' => 'ASC',
'post__not_in' => array(),
'meta_key' => '',
'meta_value' => '',
'posts_per_page' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => array( 'publish' ),
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
);

$query_args = wp_parse_args( $expected_query_args, $default_args );

$this->assertSameSets( $query_args, $filter_args[0][0], 'Unexpected $query_args for get_pages_query_args filter' );

$defaults = array(
'child_of' => 0,
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => array(),
'include' => array(),
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'parent' => -1,
'exclude_tree' => array(),
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish',
);

$parsed_args = wp_parse_args( $args, $defaults );
$this->assertSameSets( $parsed_args, $filter_args[0][1], 'Unexpected $parsed_args for get_pages_query_args filter' );
}

public function data_get_pages_args() {
return array(
'default' => array(
'args' => array(),
'expected_query_args' => array(),
),
'exclude' => array(
'args' => array( 'exclude' => array( 1, 2, 4 ) ),
'expected_query_args' => array( 'post__not_in' => array( 1, 2, 4 ) ),
),
'post status' => array(
'args' => array( 'post_status' => 'draft' ),
'expected_query_args' => array( 'post_status' => array( 'draft' ) ),
),
'number' => array(
'args' => array( 'number' => 99 ),
'expected_query_args' => array( 'posts_per_page' => 99 ),
),
'meta query' => array(
'args' => array(
'meta_key' => 'foo',
'meta_value' => 'bar',
),
'expected_query_args' => array(
'meta_key' => 'foo',
'meta_value' => 'bar',
),
),
'post parent number' => array(
'args' => array( 'parent' => 5 ),
'expected_query_args' => array( 'post_parent' => 5 ),
),
'post parent array' => array(
'args' => array( 'parent' => array( 5 ) ),
'expected_query_args' => array( 'post_parent__in' => array( 5 ) ),
),
'offset' => array(
'args' => array( 'offset' => 2 ),
'expected_query_args' => array( 'offset' => 2 ),
),
'authors' => array(
'args' => array( 'authors' => 2 ),
'expected_query_args' => array( 'author__in' => array( 2 ) ),
),
'sort order' => array(
'args' => array( 'sort_order' => 'DESC' ),
'expected_query_args' => array(
'order' => 'DESC',
'orderby' => array( 'post_title' => 'DESC' ),
),
),
);
}

/**
* @ticket 12821
*/
Expand Down

0 comments on commit 1825c75

Please sign in to comment.