A guide to the Query Posts widget

Query Posts is a widget that allows you to display posts in any way you want.

This is the first official release of the plugin. Much of it is experimental, and I do value feedback on what could make it better. Although it is experimental, the plugin is very stable. Follow this guide to get your widget up and running. If something's not covered here, there are plenty of links with extra information to help you along.

How to install the plugin

  1. Uzip the query-posts.zip folder.
  2. Upload the query-posts folder to your /wp-content/plugins directory.
  3. In your WordPress dashboard, head over to the Plugins section.
  4. Activate Query Posts.

Widget Settings

There are several settings for the widget. You need to know how the query_posts() function works to use them. You need at least a very basic understanding of its parameters to even use this widget.

Ordering and numbering
showposts — number of posts
posts_per_page — how many posts per page
order — order posts in ascending or descending order
orderby — what the posts should be ordered by
offset — how many posts to skip over
paged — show posts from a particular page (i.e. page 1, 2, 3, etc.)
Post and page parameters (Reference)
p — post ID
name — post slug
page_id — page ID
pagename — page path
post__in — comma-separated array of post IDs
post__not_in — comma-separated array of post IDs
Sticky post parameters (Reference)
caller_get_posts — true or false (true excludes stickies)
Category parameters (Reference)
cat — category ID(s)
category_name — category name/slug
category__and — array of comma-separated category IDs
category__in — array of comma-separated category IDs
category__not_in — array of comma-separated category IDs
Tag parameters (Reference)
tag — tag slug(s)
tag__and — array of comma-separated tag IDs
tag__in — array of comma-separated tag IDs
tag_slug__in — array of comma-separated tag slugs
tag_slug__and — array of comma-separated tag slugs
Author parameters (Reference)
author_name — user nicename
author — user ID
Time parameters (Reference)
year — integer
monthnum — integer
w — integer
day — integer
hour — integer
minute — integer
second — integer
Custom field parameters (Reference)
meta_key — the name of the custom field key
meta_value — the value of the custom field
meta_compare — operator to test the meta_value.

Creating a completely widgetized page template

This is not required but could be fun to do.

To truly understand how powerful this plugin is, you might want a widgetized page template, or several. My Hybrid theme has one of these. I'll give you a quick tutorial here. For more information on page templates, read this tutorial on page templates.

The first thing you want to do is create a new widget section. So, open your theme's functions.php file and add this code:

<?php
register_sidebar( array(
	'name' => 'Widgets Template',
	'id' => 'widgets-template',
	'before' => '<div id="%1$s" class="widget %2$s widget-%2$s">',
	'after' => '</div>',
	'before_title' => '<h3 class="widget-title">',
	'after_title', => '</h3>'
) );
?>

Save your functions.php file.

Then, you need to create a new file in your theme's root folder named page-widgets.php. The best method is to copy your theme's page.php file and mold this code into it. Here is an example:

<?php
/*
Template Name: Widgets
*/

get_header(); ?>

	<div class="content">

		<?php dynamic_sidebar( 'widgets-template' ); ?>

	</div>

<?php get_footer(); ?>

Plugin Support

I run a WordPress community called Theme Hybrid, which is where I fully support all of my WordPress projects, including plugins. You can sign up for an account to get plugin support for a small yearly fee ($25 USD at the time of writing).

I know. I know. You might not want to pay for support, but just consider it a donation to the project. I am fully employed through my work with WordPress, so to continue making cool, GPL-licensed plugins and having the time to support them, I must pay the bills.

Copyright & License

Query Posts is licensed under the GNU General Public License, version 2 (GPL).

This plugin is copyrighted to Justin Tadlock.

2009 © Justin Tadlock