I'm looking for a way to ensure a query always returns posts, even if the original query was empty. The query is a custom post type archive query, and my goal is to change a query var if the query has no results.

In the pre_get_posts filter, $query->have_posts() always returns no posts, so I can't find a way to do this – is there any way to predict whether a query will have results in the pre_get_posts filter?

  • i'd say no. as the name of the action suggests (aka. pre) it runs BEFORE the query, so there are no results, yet.. so you would have to go in after the query run.. – honk31 Oct 25 '18 at 15:28
  • You have access to the query vars as early as parse_request, run the query manually and see what the results are, then you can reset things as needed. – Milo Oct 25 '18 at 15:35

Your Answer

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

Browse other questions tagged or ask your own question.