WordPress.org

Make WordPress Core

Opened 18 months ago

Last modified 13 days ago

#39708 new enhancement

Provide suppress_actions argument on WP_Query::get_posts or apply suppress_filters to pre_get_posts

Reported by: aubreypwd Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.1
Component: Query Keywords:
Focuses: Cc:

Description

<?php
        public function get_posts() {
                global $wpdb;

                $this->parse_query();

                /**
                 * Fires after the query variable object is created, but before the actual query is run.
                 *
                 * Note: If using conditional tags, use the method versions within the passed instance
                 * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
                 * like is_main_query() test against the global $wp_query instance, not the passed one.
                 *
                 * @since 2.0.0
                 *
                 * @param WP_Query &$this The WP_Query instance (passed by reference).
                 */
                do_action_ref_array( 'pre_get_posts', array( &$this ) );

                // Shorthand.
                $q = &$this->query_vars;

I'm proposing that, just like we have $q['suppress_filters'] that we add a $q['suppress_actions'] to prevent actions like pre_get_posts from running (which is essentially a filter) on my query (if I want).

We could also apply $q['suppress_filters'] to the pre_get_posts action since the user can modify the query since it's passed by reference, and may not create another argument, but it is not really a filter.

I'm not sure how this idea could be problematic, nor can I think of any reason why we couldn't do anything like this, but I thought I'd at least open it up for discussion and see what becomes of it. I guess my question really is why can I suppress filters but not actions?

Change History (1)

#1 @jason_the_adams
13 days ago

As much as I wish there was better consistency in queries, I believe this would be an issue of backwards compatibility. The object is passed by reference to the pre_get_posts filter, meaning even though plugins/themes couldn't affect the rest of the hooks, they certainly are able to modify the query by changing the WP_Query instance at this point.

So if that hook were to be included in the hook suppression, existing code which changes the query without worrying about the rest of the hooks are available (or checking $wp_query->get('supress_filters')) would immediately break.

Note: See TracTickets for help on using tickets.