Using by reference of WP_Query in posts_search filter hook?
-
Working on a plugin, I was trying to change the WP_Query fired by the requested page.
The query itself is created based on several _REQUEST parameters I can use to detect and manipulate because they are unique to my plugin.
At first I thought to use the “posts_search” filter hook. This hook seemed promising as it is passing the WP_Query object by reference (using “&$this“).I managed to determine and change the WP_Query object to reflect the new query parameters for my purpose.
Unfortunately, this approach didn’t work because the changed WP_Query object was not used by the remainder of the get_posts() method. Instead, it is using $q, which is the shorthand for query_vars: “$q = &$this->query_vars“.Does anyone know how to properly use the by reference value of the WP_Query object for this purpose of manipulating the query settings?
BTW, the same applies to other filter hooks in the get_posts() method: “posts_where“, “posts_where_request“, “posts_request” and “posts_results“.
- The topic ‘Using by reference of WP_Query in posts_search filter hook?’ is closed to new replies.