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'.
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.
$wp_the_query
global which I don't think is ever defined in the admin :-)
Commented
Feb 9, 2018 at 15:10
is_admin()
is completely legit and absolutely necessary when dealing withpre_get_posts
.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.