Customizing the loop in WordPress
Opening discussion of Loop flow of control mentioned that the main workhorse for data selection is the get_posts() method of the WP_Query object. In the most cases, when you want to build a custom Loop, then you will build your own WP_Query object and reference it explicitly. Instead, The lower-level query_posts() and get_posts() functions you can use and not to be confused with the methods within the WP_Query object of the same name, to maintain the output of the default query that was passed into your Loop. Both of the query_posts() and get_posts() use the WP_Query class to retrieve the content. Now look at the lower level approaches and discuss how and where you should — and shouldn’t — use them, but you will start with a discussion of how you build a custom query object in wordpress.
Using the WP_Query Object in WordPress
After WordPress is handed a URL to parse by the web server, then it goes to work take a part the tokens in that URL and change them into parameters for a database query. when manipulating your own WP_Query then what happens.
The WP_Query is a class defined in WordPress and it makes very easy to create your own custom Loops. Both of the query_posts() and get_posts() use the WP_Query class to retrieve or fetch the WordPress content from database. After using the query_posts(),$wp_query is used as an instance of WP_Query it is the global variable, $wp_query the default data store for several operations. The Custom Loops can used anywhere in your theme template files and it used to display different types of content in you theme; On separate instances of a WP_Query variable they must built.
After you create a new WP_Query object, then it’s instantiated with some default functions for building the queries in database, to get posts execute the query, then parsing the parameters out of a URL. Whatever, you can use these built-in object methods for construct your own parameter strings in wordpress, to generate or creating custom loops that extract whatever specific content you need for that point in your Loop.
Below a following is an example of a custom Loop that is displaying the three most recent posts on your website or blog:
$myPosts = new WP_Query( ‘posts_per_page=3’ );while ( $myPosts->have_posts() )
<!– do something –>
<?php endwhile; ?>
Instead than using the simpler have_posts() and the_post() method calls that you saw in the basic Loop, and this custom loop calls the methods of the newly created WP_Query object it is $myPosts. Explicit invocation shown here and the default have_posts() call are the functionally equivalent; have_posts(), for example, is simply calling the $wp_query->have_posts() by using the global query variable for the default query — and that is, the one bring from parsing the URL handed to WordPress by the web server.
Now going into your default Loop from the URL used to raise the WordPress; another additional step is that takes the URL and parses it into an specific query string by using the parse_query() method of the query object. Your own custom Loop when you built, you set the parameters if you want to control the query. Another detail on what happens inside the query functions:
➤$myPosts->query() when calling this method it converts the parameters into an SQL statement $myPosts->get_posts() by this function, and then executes the query against the MySQL database and get or extracts the content that you have requested.
➤The query call sets up the conditional tags such as is_home() and is_single() that are depend upon what type of page will be displayed and that page content quantity.
➤Then array of the posts returned by the query is cached by WordPress that why the future references to the same query not generate the additional database traffic for the query.