A guide to the Query Posts widget

Query Posts is a widget that allows you to display posts in any way you want. It is a revolution in how users interact with their site, allowing them to have the power of more-advanced developers at the tip of their fingertips.

WordPress is a wonderful CMS to work with, but for end users, it can sometimes be a daunting task trying to make things work just they want. Query Posts makes WordPress a more robust, yet intuitive, platform for users to work from.

How to install the plugin

  1. Unzip 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.

What does the widget do?

Basically, it is a wrapper for the query_posts() function in WordPress. This is a fairly standard function used, but it requires that end users dive into code to use it. This plugin provides an interface so that the average user can interact with this function without having to learn the intricacies of the query_posts() template tag and making it work with The Loop.

Query Posts is a widget that allows you to select from several options on what posts you'd like to display. The plugin handles the rest, even the output of your selected data.

Widget Settings

There are several settings for the widget. Understanding how the query_posts() function works will help tremendously in setting up this widget.

title

Set a title for your widget. There's a checkbox in the settings to display this.

orderby

Criteria by which you want to order your posts.

order

The order in which your posts should appear.

post_status

A multiple select box that allows you to only display posts with a specific status.

post_type

A multiple select box that allows you to dispaly specific post types.

posts_per_page

Number of posts to display.

offset

The number of posts to skip over before displaying your posts.

paged

A number that represents a specific page of posts. For example, you could list posts that should only be on page 2.

post_parent

ID of the parent post for which you want to list its children.

post_mime_type

Multiple select box for showing posts with a specific mime type. This is generally only useful for attachments.

meta_key

Select a custom field (meta) key to list posts by. Only posts with this meta key will be shown.

meta_value

Select a custom field (meta) value to list posts by. Only posts with this meta value will be shown.

meta_compare

A comparison value to decide how to list posts when listing them by meta key and value.

author

Comma-separated list of author IDs.

year

A four-digit year to select posts from.

monthnum

A select box for a month to show posts from.

w

A select box for a week of the year to show posts from.

day

A select box for a day of the week to show posts from.

hour

A select box for an hour of the day to show posts from.

minute

A select box for a specific minute to show posts from.

second

A select box for a specific second to show posts from.

Disable sticky posts

Check to make sure no sticky posts appear at the top of the list.

Reset query

Resets the query used to what the current WordPress query is. Use this if you're having trouble is conditional tags in your theme.

Enable widget title

Check this to display the widget title at the top of the widget.

entry_container

Select box for which to wrap each post shown. Note that the "widget" option will make each post an individual widget on display.

post_class

An additional CSS/HTML class for individual posts.

Enable post thumbnails

Whether to show an image along with each post. If you have the Get the Image plugin installed or a theme that has it packaged, the get_the_image() function will be used. Otherwise, the the_post_thumbnail() WordPress function will be used.

size

The size of the image you'd like to show.

Enable entry titles

Whether to display the post title.

entry_title

The HTML element to wrap the entry title in on display.

byline

A shortcode-ready area for showing a post byline under the post title.

entry_content

Whether to display the full post content, excerpt, or no content at all.

The text to display for the <!--more--> link when showing the full post content.

Whether to display the paginated list of pages when using the <!--nextpage--> short tag.

entry_meta

A shortcode-ready text input for displaying post meta information after the post content.

error_message

A custom error message to display when no posts are found. This is shortcode-ready.

post

Comma-separated list of post IDs to show.

page

Comma-separated list of page IDs to show.

attachment

Comma-separated list of attachment IDs to show.

category

Comma-separated list of category IDs to show.

post_tag

Comma-separated list of tag slugs to show.

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. To continue making cool, GPL-licensed plugins and having the time to support them, I must pay the bills.

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

This plugin is copyrighted to Justin Tadlock.

2009 – 2010 © Justin Tadlock. All rights reserved.