WordPress Custom Post Types Get Into The Loop

WordPress started a new era with the Custom Post Types for developers in the WordPress environment. The possibilities are numerous and primarily from the knowledge of the developer dependent. Nevertheless, there are so many tutorials how to use Custom Post Types in WordPress, but that is not enough - at least not in most cases and therefore are various other steps necessary to make the use of CPT more efficient and smooth.

In this article I would like to briefly explain how to get content of Custom Post Types in the loop of WordPress. This is not a complete guide, but please feel free to add tips, critics, hints in our comment area.



The screenshot is an example of the use of CPT, hereby were the Plugins Archive und Snippet created, they have different tasks and via CPT tailored to the requirements you need.

In the first case, the syntax is shown how to use the query and passes through the type of the CPT via parameters. In a template of the theme it ensures that only the CPT will be read. All other parameters of the query remain untouched.

query_posts( 'post_type=my_post_type' )

Another solution differs essentially only in style and that several CPTs are passed, wich are in an array.

global $query_string;
parse_str( $query_string, $args );
$args['post_type'] = array( 'my_post_type', 'my_second_post_type' );
query_posts( $args );

To complete the loop of WP and not to limit, the array is added to the parameter of the post_type. So only via the array array_merge the array is added to another array.

global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'my_post_type' ) );
query_posts( $args );

Furthermore you can define your own queries, especially if you want to use them in your own variables, either in your Themes or Plugins.

$args = array(
	'post_type' => 'my_post_type',
	'post_status' => 'publish',
	'posts_per_page' => -1
);

$posts = new WP_Query( $args );
if ( $posts -> have_posts() ) {
	while ( $posts -> have_posts() ) {
...
	}
}
			
wp_reset_query();

At the end a solution that has a particular charm that you control it via Plugin and turn it off easily while testing. Also, to implement the application in a Plugin is simple or as an extension of an existing Themes via Childtheme or Plugin. About the Hook pre_get_posts you can adjust various things of the query, including the CPT.
In the example I have via conditional tags diverse conditions implemented, so that you can see, how it responds to the output in the frontend.

// $this? - example was used in class-structures
// add custom post type to wp loop
add_filter( 'pre_get_posts', array( $this, 'add_to_query') );

// ads to query
function add_to_query( $query ) {

	if ( is_admin() || is_preview() )
		return;
	
	if ( ! isset( $query -> query_vars['suppress_filters'] ) )
		$query -> query_vars['suppress_filters'] = FALSE;

	// conditional tags for restrictions
	if ( is_home() || is_front_page() && ( FALSE == $query -> query_vars['suppress_filters'] ) ) {
		$query -> set( $this -> my_post_type, array( 'post', $this -> get_textdomain() ) );

	return $query;
}

Comments are closed.

4 comments

  1. Rai

    Very nice post and it is true, many tutorials are out there check this one http://www.odharma.com/2010/07/creating-a-teachers-directory-with-wordpress-3-0/ if you need to construct a directory.

    Also in the example above how do you set up for see the CPT in the administration window? and add values there.

    Thanks.

  2. Aaron D. Campbell

    It looks like you're using $this outside the context of an object, so you'll probably get an error using that last code snippet.

    Also, the first snippets seem to run query_posts() again from the theme. This means that WordPress is setting up and running a query and then you're running a second query, essentially doubling the effort. Instead, using the pre_get_posts filter to merge your post type into the existing list of post types, something like this (not fully tested, but it should work):

    function add_post_type_to_query( $q ) {
    	if ( /* Whatever conditions you want to be met to add post type */ ) {
    		if ( empty($q->query_vars['post_type']) ) {
    			$q->query_vars['post_type'] = array('post','my-post-type');
    		} elseif ( 'any' == $q->query_vars['post_type'] ) {
    			return;
    		} else {
    			$q->query_vars['post_type'] = (array)$q->query_vars['post_type'];
    			$q->query_vars['post_type'][] = 'my-post-type';
    		}
    	}
    }
    add_action( 'pre_get_posts', 'add_post_type_to_query' );
    

  3. Frank

    @Aaroon: thanks, you have right; but my examples get often from uses in classes and i forgott to clean to use the copy; but i think, our readers can also see this and use the source rightly.

  4. Design Dude

    Thank you! I've been struggling with this for 2 days solid. You're right, the other tutorials don't effectively cover this issue of getting CPT into the loop. I don't understand your code (yet) but it works. This should be added to the codex.