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'.
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'.
Share Improve this question asked Feb 9, 2018 at 14:12 Paul CanningPaul Canning 1194 bronze badges 3 |1 Answer
Reset to default 1Using 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.
is_admin()
is completely legit and absolutely necessary when dealing withpre_get_posts
. – Max Yudin Commented Feb 9, 2018 at 14:15is_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. – Jacob Peattie Commented Feb 9, 2018 at 14:22