0

I am modifying a query using the pre_get_posts filter, but it is affecting the backend page listing when I only want it to affect the frontend query.

I can check using is_admin() but that seems a bit 'hacky'.

3
  • 2
    is_admin() is completely legit and absolutely necessary when dealing with pre_get_posts.
    – Max Yudin
    Feb 9, 2018 at 14:15
  • I think is_admin() has a poor reputation because people misuse it thinking that it means 'is user an administrator', but it's perfectly legitimate for this use, and is in fact suggested in the Codex. Feb 9, 2018 at 14:22
  • Hey Paul could you please add your current code to your question for clarification?
    – leymannx
    Apr 14, 2019 at 12:24

1 Answer 1

1

Using is_admin() is not hacky, as pointed out in the comments.

You should definitely check for is_admin() when modifying frontend queries. If you're modifying the main query, also use the is_main_query() function / method.

2
  • Is is_main_query() for the frontend or backend? Feb 9, 2018 at 14:27
  • It's for the front end as it checks the $wp_the_query global which I don't think is ever defined in the admin :-)
    – swissspidy
    Feb 9, 2018 at 15:10

Your Answer

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

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