admin管理员组文章数量:1023773
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.
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-
2
is_admin()
is completely legit and absolutely necessary when dealing withpre_get_posts
. – Max Yudin Commented 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. – Jacob Peattie Commented Feb 9, 2018 at 14:22 - Hey Paul could you please add your current code to your question for clarification? – norman.lol Commented Apr 14, 2019 at 12:24
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.
本文标签: Modify WPQuery using pregetposts but only for frontend query
版权声明:本文标题:Modify WP_Query using pre_get_posts but only for frontend query? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745591360a2157908.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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