A fresh mix of code snippets and tutorials

Query custom post types

Here’s how to query custom post types with WordPress 3 and better.

Let’s say our custom post type is named “books”. Here is how to create a list of posts from the books custom post type:

	<?php global $wp_query;
	$wp_query = new WP_Query("post_type=books&post_status=publish");
	while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
	<li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
	<?php endwhile; ?>

This code is designed to work in any of WordPress theme files, preferably in a location that makes sense.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress