最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Modify WP_Query using pre_get_posts but only for frontend query?

programmeradmin3浏览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'.

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 with pre_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
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论