[wp-trac] [WordPress Trac] #12821: Merge get_posts() and get_pages()
WordPress Trac
noreply at wordpress.org
Wed Apr 26 11:17:00 UTC 2023
#12821: Merge get_posts() and get_pages()
-------------------------------------------------+-------------------------
Reporter: mikeschinkel | Owner:
| spacedmonkey
Type: enhancement | Status: reopened
Priority: normal | Milestone: 6.3
Component: Posts, Post Types | Version: 3.0
Severity: normal | Resolution:
Keywords: needs-dev-note has-patch has-unit- | Focuses:
tests needs-testing dev-feedback commit | performance
-------------------------------------------------+-------------------------
Comment (by marianne38):
@spacedmonkey Here is a small test to see our issue :
{{{#!php
<?php
public function test_get_pages_with_taxonomy_on_page() {
wp_set_current_user( 1 ); // For "assign_terms" access
with "tax_input" in wp_insert_post().
register_taxonomy( 'language', 'page' );
$term_en = self::factory()->term->create( array(
'taxonomy' => 'language', 'name' => 'english' ) );
$term_fr = self::factory()->term->create( array(
'taxonomy' => 'language', 'name' => 'french' ) );
$page_en = self::factory()->post->create( array(
'post_type' => 'page', 'tax_input' => array( 'language' => array(
$term_en ) ) ) );
$page_fr = self::factory()->post->create( array(
'post_type' => 'page', 'tax_input' => array( 'language' => array(
$term_fr ) ) ) );
add_filter(
'get_pages',
function ( $pages, $args ) {
static $once = false;
if ( $once ) {
return $pages;
}
$once = true; // Avoid infinite loop.
$r = array(
'post_type' =>
$args['post_type'],
'post_status' =>
$args['post_status'],
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' =>
'language',
'field' =>
'slug',
'terms' =>
$args['language'],
'operator' => 'NOT
IN',
),
),
);
$args['exclude'] = array_merge(
wp_parse_id_list( $args['exclude'] ), get_posts( $r ) );
$numbered_pages = get_pages( $args );
$pages = ! $numbered_pages ?
$pages : $numbered_pages;
return $pages;
},
10,
2
);
add_action(
'parse_query',
function ( $query ) {
if ( ! isset(
$query->query_vars['language'] ) ) {
$query->set( 'language', 'english'
);
}
}
);
$pages = get_pages( array( 'language' => 'french' ) );
$this->assertCount( 1, $pages );
$this->assertSame( $page_fr, $pages[0]->ID );
}
}}}
The {{{get_pages()}}} filter is a simplified version of what we did to
sort the pages by language.
The action on {{{parse_query}}} represents our filter on the current
language.
This test works with **WP 6.2** and not with **WP 6.3**.
We had to do this because {{{get_pages()}}} was not working as
{{{get_posts()}}}.
Now that {{{get_pages()}}} uses a {{{WP_Query}}}, our pages are always
filtered by the current language, even if we pass a language argument to
{{{get_pages()}}} like this :
{{{get_pages( array( 'language' => 'english' ) )}}}
A possible solution would be if {{{get_pages()}}} could propagate the
arguments it gets directly to the {{{WP_Query}}}, we wouldn't have to do
anything on our side (we could do without the {{{get_pages}}} filter).
Or, as you suggested above, add a filter that would allow to filter the
{{{$query_args}}} of the {{{WP_Query}}} in order to be able to propagate
ourselves the {{{language}}} argument passed to {{{get_pages()}}}.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/12821#comment:81>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list