wp()
Sets the main query (WordPress environment).
What exactly does wp() do?
-
sets or overrides global variables: $wp_query, $wp_the_query;
-
sets the header title (including "error 404");
-
creates a database query based on the provided arguments (parameter $query_vars), filling global variables with the results of the query;
-
sets global variables such as: $query_string, $posts, $post, $request, $more and $single (for is_singular()), $authordata (for is_author()) and all variables of $wp_query:
The function works based on the class WP{}
For a better understanding of how this function works, read the description of the hook request
wp() can be used to completely rewrite the main WordPress query. That is, calling the function with parameters will change many global variables including $wp_query, which means the main output loop will change.
I do not recommend using the function if you are not sure of what you are doing. To change the main loop, there is query_posts(), after which you can return to the original query by cleaning up changes with the function wp_reset_query(). In the case of wp(), this will no longer be possible.
At the end of the function's execution, the hook-action wp is triggered.
No Hooks.
Returns
null. Returns nothing.
Usage
wp( $query_vars );
- $query_vars(string/array)
- Request parameters. Accepts all the same parameters as WP_Query.
Default: Default parameters
Examples
#1 Demo example of how this function works
Suppose we have a static page. At the very beginning of the page code, call the function as follows:
<?php wp('author=1&orderby=title&order=ASC'); ?>
Then comes the standard loop output.
As a result, the loop will display the author's posts with ID = 1, sorted by post title.
Also, the is_author() conditional tag will be triggered on this page, not is_single().
It's just demo example - not recommended to use it in you project. wp() function calls by WP itself and not considered to use somewhere else.
Notes
- Global. WP.
$wpCurrent WordPress environment instance. - Global. WP_Query.
$wp_queryWordPress Query object. - Global. WP_Query.
$wp_the_queryCopy of the WordPress Query object.
Changelog
| Since 2.0.0 | Introduced. |
wp() wp code WP 6.9.1
function wp( $query_vars = '' ) {
global $wp, $wp_query, $wp_the_query;
$wp->main( $query_vars );
if ( ! isset( $wp_the_query ) ) {
$wp_the_query = $wp_query;
}
}