Define Your Own WordPress Loop Using WP_Query
- Basic WordPress template file
- Tags Are Not Categories – Got It?
- How to write a Wordpress plugin: episode 1 – getting started
- wordpress Plugins top 50
- My Widget – example WordPress widget
- To know wordpress
- Welcome to ajaxtime.com !
We all know what the WordPress Loop is right? If not, there are many great tutorials around the web that explain the WordPress Loop.
One of the easiest ways to navigate and manipulate the loop is to use the function called query_posts. Nathan Rice calls it a WordPress developers best friend.
When you use query_posts, however, you risk the following:
- Potential to interfere with plugins which make use of the Loop.
- Potential to invalidate WordPress conditional tags.
- Having to deal with resetting, rewinding, offsetting…
I say skip query_posts. In a way you’ll still be using it, but the better (and sometimes easier) technique is to instantiate your own WP_Query object and create your own loop.
Creating Your Own Loop With WP_Query
The first step is to instantiate your own variable using the WP_Query class.
What we’ll be doing in this example is creating a common feature on blogs, which is to display a list of the recent articles.
<?php $recentPosts = new WP_Query(); $recentPosts->query('showposts=5'); ?>
All I’ve done in the above code is defined a variable named recentPosts and instantiated an instance of WP_Query.
I then used a method of WP_Query to start a query (pretty much the same thing as using query_posts). You even use the same usage parameters as query_posts.
Now it’s time to start our own loop:
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <!-- do some stuff here --> <?php endwhile; ?>
Notice the use of the recentPosts variable to start the loop. We utilize two methods of WP_Query, which is have_posts and the_post. You can read more about those two methods on my article Global Variables and the WordPress Loop.
The beauty of this is that once you are inside your own loop, you can use the standard post tags.
The Full Code
Here’s the full code for showing the last five recent posts using your own loop:
<h3>Recent Articles</h3> <ul> <?php $recentPosts = new WP_Query(); $recentPosts->query('showposts=5'); ?> <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul>
Update: Using Pagination
This comment is from Aaron Harun -
@Ron and Monika
If you use the query:
$recentPosts->query('showposts=5'.'&paged='.$paged);
it will automatically page the wury based on the page number passed
through the url eg “/page/4″
However, this doesn’t work with the “post_nav_link” function because it
only checks the $wp_query variable. To get around this you have to
trick the function by switching $wp_query on it. Add the first block
before your custom loop and the second after to achieve this effect.
<?php //query_posts('paged='.$paged); $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('showposts=5'.'&paged='.$paged); ?>
<?php $wp_query = null; $wp_query = $temp;?>
Thanks Aaron for the contribution.
Conclusion
Defining your own loop using WP_Query is an easy way to run your own custom queries without interfering with the default Loop. It’s also a great way to run multiple loops that are completely independent of each other.
WordPress Resources mentioned:
Other News
- Define Your Own WordPress Loop Using WP_Query
- Basic WordPress template file
- Discover the Clone Stamp
- A decent vocabulary is the key to profitable SEO (LSI and hitting the long tail)
- Working with Layers Part I: Layer Sets
- PHP and MySQL 5
- Basic Image Enhancement
- PHP: Hypertext Preprocessor
- How to SEO your site: Episode 4 – Social news as an SEO tool
- To know wordpress
-

1.CSS+DIV+HTML
Do a framework for the website
Read More | Postd by Ltm.ming -

2.PhotoShop CS
To Beautify the site
Read More | Postd by Ltm.ming -

3.JavaSprict-Script
For User experience
Read More | Postd by Ltm.ming -

4.Programming language
php is very powerful
Read More | Postd by Ltm.ming -

5.SQL & MYSQL
For Information Storage
Read More | Postd by Ltm.ming

PHP and MySQL 5
Beginning PHP 5 and MySQL: From Novice to Professional offers a comprehensive introduction to two of the most popular open source technologies on the planet: the PHP scripting language and the MySQL database server.
Read More | Postd by Ltm.ming
Base AJAX Codes and Examples
Make your Web pages stand out above the noise with JavaScript and the expert instruction in this much-anticipated update to the bestselling JavaScript Bible.
Read More | Postd by Ltm.ming
Mastering CSS with DW CS3
Durch Integration von Datenbankanbindung und Skriptsprachen wird Dreamweaver CS 4 zum alltagstauglichen Werkzeug für die Erstellung einfacher Redaktionssysteme.
Read More | Postd by Ltm.ming
Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites.
Read More | Postd by Ltm.ming
MySQL 5.0 Certification Study Guide
This book is a very comprehensive guide to what MySQL offers and can teach you. br / br /Would definitely recommend for anyone needing a great book about MySQL.
Read More | Postd by Ltm.ming