[wp-trac] [WordPress Trac] #12821: Merge get_posts() and get_pages()

WordPress Trac noreply at wordpress.org
Tue May 16 18:10:28 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 kenwins):

 The provided code includes a test function that demonstrates an issue with
 the get_pages() function in WordPress. The test involves registering a
 custom taxonomy called "language" and creating two pages assigned with
 different language terms. The objective is to retrieve pages based on the
 language argument passed to get_pages(). However, in WordPress 6.3, a
 change in the behavior of get_pages() results in pages being always
 filtered by the current language, disregarding the language argument
 provided to get_pages().

 Proposed Solution:
 To address this issue, the following solution is proposed:

 Solution: Adding a Filter to Modify WP_Query Arguments
 To allow for modifying the $query_args of the WP_Query instance used
 within get_pages(), we can introduce a filter. This filter enables us to
 intercept and modify the query arguments, including the language argument,
 before the query is executed.

 Here's an example implementation of this solution:

 {{{
 public function test_get_pages_with_taxonomy_on_page() {
     wp_set_current_user( 1 );

     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_query_args',
         function ( $query_args, $r ) {
             $query_args['tax_query'][] = array(
                 'taxonomy' => 'language',
                 'field'    => 'slug',
                 'terms'    => $r['language'],
                 'operator' => 'IN',
             );

             return $query_args;
         },
         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 );
 }
 }}}
 In this solution, we introduce the get_pages_query_args filter, which
 allows us to modify the query arguments before executing the WP_Query
 inside get_pages(). We add a tax query to include pages with the specified
 language term. By implementing this solution, we can ensure that the
 language argument is correctly handled and respected in the get_pages()
 query[https://e-kranreviewblog.blogspot.com/2022/11/review.html .] Note:
 This solution assumes that you have already updated to WordPress version
 6.3, where the behavior of get_pages() has changed.
 Solution 2: Add Filter to Modify WP_Query Arguments
 Another solution involves introducing a filter that allows for modifying
 the $query_args array of the WP_Query instance used within get_pages(). By
 implementing this filter, we can intercept and modify the arguments,
 including the language argument, before the query is executed.

 Here's an example implementation of this solution:

 {{{
 public function test_get_pages_with_taxonomy_on_page() {
     wp_set_current_user( 1 );

     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_query_args',
     function ( $query_args, $r ) {
         $query_args['tax_query'][] = array(
             'taxonomy' => 'language',
             'field'    => 'slug',
             'terms'    => $r['language'],
             'operator' => 'IN',
         );

         return $query_args;
     },
     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 );
 }
 }}}

 In this solution, we introduce the `get_pages_query_args` filter, which
 allows us to modify the query arguments before executing the `WP_Query`
 inside `get_pages()`. We add a tax query to include pages with the
 specified language term.

 Both solutions aim to resolve the issue by ensuring that the language
 argument is correctly handled and respected in the `get_pages()` query.
 You can choose the solution that best suits your requirements and
 integrate it into your codebase accordingly.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/12821#comment:90>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list