If I want to modify the query, I can use pre_get_posts filter. But what if I want to loop through the queried posts before they are ran through the The Loop? Is there a filter/action hook for that? I want to find out if all the queried posts belong to same term and then do something if they are not. Please note that creating a custom Query is not an option.

1 Answer 1


You can use the the_posts filter to loop through the results before they get returned.

Proof of concept:

function test_the_posts($a) {
  var_dump($a); die;

I am fairly certain (though I haven't tested it) that you could pretty easily break pagination and probably other things by fiddling with that, so be careful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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