I'd like to be able to hook into WP_Query in such a way that I can add a custom argument to my WP_Query like:

$q = new WP_Query([
  'my_cool_arg' => [ 1, 2, 3 ]

and then access this and modify the where part of the generated SQL query. I am aware of the posts_where filter, but I don't know how to access the arguments from that filter.

  • What exactly should this custom argument do Sep 10, 2015 at 13:17
  • I'm interested in a general solution to the problem. But as an example let's say I want to append an EXISTS statement to the WHERE clause that queries a custom table in the database using an IN generated from the array I provided. Generating the SQL isn't a problem for me, I just need to find a way to access the argument while modifying the query.
    – Nocklas
    Sep 10, 2015 at 14:15

1 Answer 1


Solved it. The filter posts_where takes the query object as a second parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.