Using query_posts( ) in the WordPress

Tremendous amount of customization of wordpress loop can be done by specifying the adequate set of parameters for your Loop. The WP_Query object is the most  general-purpose mechanism for extracting the content from the WordPress database, there are other lower-level methods that you’ll encounter. Function query_posts() is used to easily modify the content returned for the default WordPress Loop.  You can modify the content that is returned in $wp_query after the default database of wordpess query has executed in the database, so fine-tune the query parameters,  and re-execute the query using query_posts() function. The downside to calling query_posts() in this fashion is that the previously cached results from the default query are discarded, so you’re incurring a database performance hit to use this shortcut. The query_posts() function should be placed directly above the start of the Loop:

query_posts( ‘posts_per_page=5&paged=’.$paged );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
//loop content (template tags, html, etc)

Above example tells the WordPress to display only five posts on the page. The calling query_posts() overwrites the original post content extracted for the WordPress Loop. Means that any content of you were expecting to be returned before using the query_posts() it will not be returned.  Let suppose, The wordpress for a category page is passed the URL to WordPress at, none of the webdesign category posts will be in the post list after
the query_posts() has been called it unless one is in the five most recent posts of WordPress. The query parameters established by the URL parsing and you explicitly overwrite it and default processing when you pass the query string to the query_posts() method.

For avoid losing your original Loop content, then you can save the parsed query parameters by using the global variable this

// initialize the global query_string variableglobal $query_string;
// keep original Loop content and change the sort order
query_posts( $query_string . “&orderby=title&order=ASC” );

In this example, Still you are seeing all of your webdesign category posts, it would be ordered the alphabetically by ascending title. The technique of this loop is used to modify the original Loop content without losing that original content.
Anohter thing is that you can also pack all of your query_posts() parameters in an array, so making it easier to manage the posts.
Below is an example that sticky posts how to retrieve that set in WordPress using an array it is called $args to store the parameter values:
$args = array(
‘posts_per_page’ => 1,
‘post__in’ => get_option( ‘sticky_posts’ )
query_posts( $args );

Sticky post if no found, then the latest post will be returned instead of the sticky posts. And the query_posts() method is used to modify the main page Loop only. There is not intended to create the additional custom Loops.  A slight change to the default query if you want to make — for example, Every displayed page you want to adde posts of a specific category or tag— so the query_posts() approach is a shortcut.

Remember, it’s not without side effects or warnings:

➤ query_posts() has modifies the global variable $wp_query and has other side effects of it. It shouldn’t be used inside the Loop  and It should not be called more than once.  The call to query_posts() before post processing has started, the extra parameters when added to the query string but before the Loop has start to step through the returned post list from the database.  query_posts() calling it more than once, or inside the Loop itself, result  can in your main Loop being incorrect and displaying unintentional content.

➤ The query_posts() unsets the global object $wp_query, it doing so, it may avoid the values of conditional tags such as is_home() or is_page(). The entire of WP_Query object instantiation sets all of the conditional tag suitably. Let’s suppose, you may find with the shortcut that of that then you have added the content to a selection that the default query found it contained only one post in the default query, so therefore is_single() is no longer valid.

➤ When Calling the query_posts() it executes another database query, invalidate all of the cached results from the first, the default query.

The database queries executed at least double the number and are incurring a performance hit for each trip back to MySQL database; by the time you get to the default Loop on the other hand the default query has already been run, There’s little chance to work around it when you are building an inside the custom main Loop.