HowTo – Using Query Posts To Display Posts In WordPress

Filed under: Wordpress Tips

wordpress_logoWhen you add posts to your WordPress Blog by default if you haven’t made changes to your theme they are assigned a Post ID number that identifies that specific post. If you do nothing else they will be displayed in order of the newest to oldest in one single group.

Because it is unfair or useless to expect your visitors to read through hundreds of posts to find something you wrote about your pet iguana 3 months ago a category system is provided to let you sort  your posts and generate a menu system for navigation.

You can assign posts to a category and you can also assign posts to Tags. When a visitor clicks on your sidebar posts menu they will be shown posts from that category. The code to display the categories is in your archive.php file. When a visitor reads a specific page on your Iguana you can display related tags at the top or bottom of your page that link to additional posts about your Iguana.

Setting up your WordPress Loop by using query_posts will allow you to modify the list of posts that are returned and either include or exclude posts based on a variety of criteria.

Using query Posts

To setup what data is retrieved in your loop you can place a query_posts before the loop.

General format is like this

[php]<?php
<strong>query_posts(‘your value here’);</strong>
while(have_posts()) { the_post();
<!– loop –>
}
?>

/[php]

<hr />

To <strong>retrieve</strong> just a single page <strong>by the ID</strong> of 5
[php] <? query_posts(‘page_id=5’); ?>[/php]

To retrieve just a single page by name

[php] <? query_posts(‘name=about’); ?>[/php]

Include or Exclude Categories

To retrieve posts only from a single category 7

[php] <?query_posts(‘cat=7’): ?>[/php]

To retrieve posts from a list of categories

[php] <? query_posts(cat=3,5,8); ?>[/php]

To exclude posts from Single category 7

[php] <? query_posts(‘cat=-7’); ?>[/php]

To exclude a list of Categories

[php] <?query_posts(‘cat=-7,-8,-9’); ?>[/php]

Order the Posts

To retrieve posts in Ascending Order

[php] <? query_posts(‘cat=-7&<strong>order=ASC</strong>’); ?>[/php]

To retrieve posts in Descending Order

[php] <? query_posts(‘cat=-7&<strong>order=DESC</strong>’); ?>[/php]

also use

orderby=author
orderby=date
orderby=category
orderby=title
orderby=modified
orderby=menu_order
orderby=parent
orderby=ID
orderby=rand
orderby=meta_value /This is an optional field set in the post

The Number of posts to get

To set the number of posts to be displayed per page

[php] <? query_posts(‘cat=-7&<strong>posts_per_page=6</strong>’); ?>[/php]

Use Meta or Custom Fields

Custom fields are inserted by you when you write the post and can be retrieved as Meta Data within the loop or in this case can be used for sorting your posts.

meta_key= The Name of the custom field
meta_value= The Value of the custom field
meta_compare= values of ‘!=’, ‘>’, ‘>=’, ‘<‘, or ‘<=’

To retrieve all the posts that have any color set (including all colors)
query_posts(‘meta_key=color’);

To retrieve only the posts that have the color set to blue
query_posts(‘meta_key=color&meta_value=blue’);

To retrieve posts where the custom field price is lower or equal to 100
query_posts(‘meta_key=price&meta_compare=<=&meta_value=100‘);

To retrieve posts based on Tags

To get posts with a single tag
query_posts(‘tag=iguana’);

To get posts from either of these tags
query_posts(‘tag=iguana,chicken’);

To get posts that belong only to ALL tags
query_posts(‘tag=mypets+chicken+iguana’);

Note

There are a number of other ways to use the query post call to your database to setup how your posts.

A great way to use this call is to display your front page as a home page or setup special category theme pages to customize how special categories display for your visitor.

For more information visit the WordPress codex:

http://codex.wordpress.org/Template_Tags/query_posts