wp query - What does $temp do?

admin2025-06-02  0

I am new in WordPress Development. And I saw a sample which uses $temp to store the old $wp_query instance before instantiating a new WP_Query. I guess it is used to return original state when showing some posts other than the current $wp_query contains. But, I would like to be sure and learn other reasons. And I wonder what is the best way of this type of operations.

I am new in WordPress Development. And I saw a sample which uses $temp to store the old $wp_query instance before instantiating a new WP_Query. I guess it is used to return original state when showing some posts other than the current $wp_query contains. But, I would like to be sure and learn other reasons. And I wonder what is the best way of this type of operations.

Share Improve this question edited Mar 10, 2019 at 13:56 Tanmay Patel 8111 gold badge7 silver badges11 bronze badges asked Mar 10, 2019 at 11:25 tafotafo 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

What any variables used in samples do depends entirely on that specific sample. If a sample assigns a query to a variable called $temp, then that's what $temp does. It doesn't mean anything else, it's just what's shown in the sample.

Guide and tutorial writers can and will create and use any variables they want. You will need to read and understand the guide to understand what they're doing, or ask the author if you're unsure. If you need to, learn basic PHP first, as it will help a great deal.

That being said, what you've described — modifying $wp_query directly and then resetting it — is something you should absolutely never do. Do not take further advice from any source that told you to do this.

If you need to query posts for your own purposes, use new WP_Query() or get_posts(), and if you need to modify the main query, use the pre_get_posts filter. These are thoroughly documented across the web, and this site, if you need to know more.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748823371a314019.html

最新回复(0)