admin管理员组

文章数量:1389865

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.

本文标签: theme developmentWhat happens to the default query when I use WPQuery