Make WordPress Core

Ticket #35816: 35816.patch

File 35816.patch, 1.4 KB (added by stevegrunwell, 9 years ago)

Patch adding after_get_posts action, written in collaboration with @lpawlik

  • src/wp-includes/query.php

     
    37883788                        $this->posts = array();
    37893789                }
    37903790
     3791                /**
     3792                 * Fires after the query has been set up and executed.
     3793                 *
     3794                 * Note: If using conditional tags, use the method versions within the passed instance
     3795                 * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
     3796                 * like is_main_query() test against the global $wp_query instance, not the passed one.
     3797                 *
     3798                 * @since 4.5.0
     3799                 *
     3800                 * @param WP_Query &$this The WP_Query instance (passed by reference).
     3801                 */
     3802                do_action_ref_array( 'after_get_posts', array( &$this ) );
     3803
    37913804                return $this->posts;
    37923805        }
    37933806
  • tests/phpunit/tests/query/setupPostdata.php

     
    380380                }
    381381        }
    382382
     383        /**
     384         * @ticket 35816
     385         */
     386        public function test_after_get_posts_is_called() {
     387                $query  = new WP_Query;
     388
     389                // Add a listener to the new 'after_get_posts' action.
     390                $action = new MockAction();
     391                add_action( 'after_get_posts', array( &$action, 'action' ) );
     392
     393                // Execute the get_posts() method.
     394                $query->get_posts();
     395
     396                $this->assertEquals( 1, $action->get_call_count() );
     397        }
     398
    383399}