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

theme development - What happens to the default query when I use WP_Query?

programmeradmin2浏览0评论

I'm working on a custom theme based on Underscores. In my archive.php I want to style the first post in the loop differently than the rest, so I'm running one WP_Query to fetch the first post, and another to get the remaining posts.

What I'm wondering is -- since I'm handling the queries myself, what happens to the default query that WordPress is running implicitly (that is, the one I'd normally be iterating over with while ( havePosts() )? Obviously for performance reasons I don't want to run any unnecessary queries, so is there a way I can stop it?

I'm somewhat familiar with the pre_get_posts hook, but I'm not sure how I would use it since, as I mentioned, I'm not just trying to modify the one default query -- I need two (unless there's a better way of doing this).

I'm working on a custom theme based on Underscores. In my archive.php I want to style the first post in the loop differently than the rest, so I'm running one WP_Query to fetch the first post, and another to get the remaining posts.

What I'm wondering is -- since I'm handling the queries myself, what happens to the default query that WordPress is running implicitly (that is, the one I'd normally be iterating over with while ( havePosts() )? Obviously for performance reasons I don't want to run any unnecessary queries, so is there a way I can stop it?

I'm somewhat familiar with the pre_get_posts hook, but I'm not sure how I would use it since, as I mentioned, I'm not just trying to modify the one default query -- I need two (unless there's a better way of doing this).

Share Improve this question asked Mar 25, 2020 at 7:47 bertdaybertday 1196 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

WordPress always queries a default set of posts when you view any sort of archive. So if you run your own query as described you’ll end up with 3 database queries instead of 1.

You should not develop your template the way you describe. The standard WordPress templates should all use the default query. You’ll be coming back as the 400th person to ask why pagination isn’t working properly if you do it this way, among other potential issues. If you want to style the first post differently there are plenty of ways to do that without querying it separately, from CSS techniques to logic in the template. If you have something specific you’re trying to accomplish, I suggest posting a new question with those details.

发布评论

评论列表(0)

  1. 暂无评论