Simple Form To Select A Category And Sort Order

There is a lot of code here, and the output doesn’t look very pretty, but it illustrates a basic form to let the Viewer select a Category and sort order for the Twenty Ten theme. Continue reading

Consistent Random Set Of Posts

A recent post on the WP support forum asked how to retrieve posts randomly, but be consistent when paginating.  In other words, the same random set would be shown on page 1 even after switching to a different page and back again.

The answer to this is to use a filter on the query to replace the ‘ORDER BY’ clause.  The MySQL RAND() function allows a seed value to the function.  Every time the same seed is supplied, the same set of rows will be retrieved for a given query. Continue reading

List Posts Alphabetically Under First Letter Of Title

Here is sample code to list posts alphabetically under the first letter of the title.

For a complete template, see this post.

Continue reading

Template Parts in WP 3.0

Use template parts in WP 3.0 for repeated blocks of code. In the example below, you would create a file called loop-custom.php which would be called from each section. Continue reading

Preserve Original Query Arguments

Codex Function_Reference query_posts #Preserving_the_Original_Query_Pagination_etc

If you want to override the original arguments, you must merge your argument array last (reverse of what is shown in Codex):

global $wp_query;
      array('cat' => 1)

List titles of all custom post types

The code below is from a post by MichaelH on the WP Support forum: Continue reading

Show posts one day at a time

The code below retrieves an array of dates which have posts and shows the posts for one day at a time. Continue reading

Filters to Modify a Query

UPDATE: See the excellent variation by keesiemeijer here: https://gist.github.com/4643765

Many times it is necessary to use a filter to modify a query.  For example, you may want to include a column that WP does not normally include in a query.  Or, you may want to do some complex selection of posts based on criteria that query_posts does not supply.  Continue reading

First and Last Post on a Page

If you want to detect the first and last post on a page (not the entire query), you can use the code below:

if (have_posts()) : while (have_posts()) : the_post();
  if( $c == 1 ) {
  } elseif ( $c == $wp_query->post_count ) ) {
  // Rest of the code for the Loop

Posts from Custom Query

This Codex article has a detailed explanation of how to create a template using a Custom Query: