最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Modify WP_Query using pre_get_posts but only for frontend query?

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far