When using custom loops its very important to work with custom wp query objects in WordPress and passing arguments, querying and retrieving data with in loop in WordPress.
How to use wp query object in WordPress:
Once WordPress generates a URL will be parsed by web server and it will be converting the given or passed parameters for a database query and retrieves the data according to declared custom loop.
In WordPress using wp_Query object we can easily manipulate your own custom WP_Query. WP_Query is a class declared or defined in WordPress which makes it very easy to create your own custom Loops in WordPress for both query_posts() and get_posts() use the
WP_Query class to retrieve the WordPress content.
When you’re using
query_posts(), the global variable
$wp_query is used as an instance of WP_Query, making
$wp_query the default data store for several operations. Custom Loops can be used where ever you want in your theme template files to display different types of content, they must build on separate instances of a
WP_Query variable. When you create a new
WP_Query object, it’s starts with some default functions for building queries and executing the query to get posts in WordPress and parsing parameters out of a URL. However, you can use these built-in object methods to construct your own parameter strings, creating custom loops that extract whatever particular content you need for that point in your Loop.
Example of WP Query Object in WordPress:
Below is an example of a custom Loop displaying the 5 most recent posts on your website:
Relatively than using the simpler
have_posts()the_post() calls that you saw in the basic Loop, this custom loop calls the methods of the newly created
WP_Query object $myPosts. The explicit invocation shown here and the default have_posts() call are functionally equivalent; have_posts(), for example, is merely calling $wp_query->have_posts() using the global query variable for the default query that is, the one generated from parsing the URL handed to WordPress by the web server.
Going into your default Loop from the URL used to invoke WordPress; there’s an additional step that takes the URL and parses it into an appropriate query string using the
parse_query() method of the query object. When you build your own custom Loop, you explicitly set the parameters you want to control the query. Here’s a bit more detail on what happens inside the query function:
$myPosts->query()converts the parameters into an SQL statement via the function
$myPosts->get_posts(), which then executes the query against the MySQL database and extracts the content you’ve requested.
- Equally important, the query call sets up the conditional tags such as
is_single()that are dependent upon the type of page displayed and the quantity of content for that page.
- The array of posts returned by the query is cached by WordPress so that future references to the same query won’t generate additional database traffic.
The key to building a powerful custom Loop is to map your content selection criteria into the right set of query parameters.