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.
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.