So I got this template which originally queried regular WP posts by their categories so that the page would show a category name for "category 1" as a headline and then posts from that category below it. The same for "category 2", "category 3", etc.

I'm trying to make it work with custom post types, so that "Post type 1" would be the headline and posts from "Post type 1" would be below it. And again the same for "Post type 2", etc. Basically the exact same thing but with custom post types instead of categories.

With the code below, I'll get the headlines to their places, but no posts appear below them. I'm a bit stuck with this and any help would be greatly appreciated.

$args = array('_builtin' => 0);
$post_types = get_post_types($args);
foreach ($post_types as $post_type) {

    $post_type_link = get_post_type_archive_link( $post_type -> $post->ID  );  
    <div class="cat_list full">
        <h3 class="h3title"><a href="<?php echo $post_type_link; ?>"><span><?php echo $post_type;  ?></span></a></h3>
        <div class="cat inline-block">
            <ul class="switch half clearfix">

    //$numberposts = admin::get_field_val('category_posts_nbr_front');  
    $numberposts = admin_options::get_values( 'front_page' , 'nr_post' );
    $post_number = 0; 

        $args = array('post_type' => array( 'make', 'model', 'price' ), 'showposts'=>100, 'orderby'=>'date');
        $type_posts = new WP_Query($args); while($type_posts->have_posts()) : $type_posts->the_post();

        if(get_post_thumbnail_id($post->ID) )
            $post_img = wp_get_attachment_image(get_post_thumbnail_id($post->ID),'62x62','' );
            $post_img = get_first_image($post->ID,'54x54');

            <li <?php if($post_number % 2 == 1) echo "class='col_2'" ?>>
                <a href="<?php echo get_permalink( $post->ID ); echo '#more'; ?>"><?php echo $post_img; ?></a>
                <h5><a href="<?php echo get_permalink($post->ID ); echo '#more'; ?>"><?php echo mb_substr(get_the_title( $post->ID ),0,BLOCK_TITLE_LEN); if(strlen(get_the_title( $post->ID ) ) > BLOCK_TITLE_LEN ) { echo ' ...'; } ?></a></h5>
                <span class="date"><?php echo mysql2date(get_option( 'date_format' ) ,$post ->post_date) ?> &nbsp; //</span>
                <span class="comm">
                    <?php if ('open' == $post->comment_status) { ?>
                        <a href="<?php echo get_permalink( $post->ID); echo '#comments'; ?>"><?php  echo $post->comment_count.' '; if($post->comment_count ==1) {_e('Comment');} else {_e(' Comments');} ?> </a>
                    <?php }else{ // comments are closed ?>
                        <a><?php _e( 'Comments Off' ); ?></a>
                    <?php } ?>  

            $post_number ++; 


            <div class="no_bottom_border"></div>
    </div> <!--  EOF cat_list--> 


} /*EOF foreach categories*/ ?>

EDIT: Well, now it displays the posts below the headlines, but without separating them for their own post types. In other words, every post made is shown under every post type headline. What gives?

1 Answer 1


I believe the problem is how you are using this code:

$args = array('post_type' => array( 'make', 'model', 'price' ), 'showposts'=>100, 'orderby'=>'date');
$type_posts = new WP_Query($args);

You may want to change it to something like this:

$args = array('post_type' => array( $post_type ), 'showposts'=>100, 'orderby'=>'date');
$type_posts = new WP_Query($args);

So that it only grabs posts for that post type in your foreach loop that loops through each post type.

Hope this helps.

  • I was thinking that must be the problem too, but with your code no posts appear at all. changing to $post_type->ID did not work either. What could be wrong?
    – Andrew
    Commented Feb 17, 2012 at 7:16
  • @Andrew try the second code I posted in my answer except change $post_type->name with just $post_type. I did some testing and it only seems to return the name so the ->name was null.
    – Jared
    Commented Feb 17, 2012 at 15:59
  • Works like a charm! Thank you very much. I tried to mess around with names and IDs but never thought about leaving them out altogether. Very nice.
    – Andrew
    Commented Feb 17, 2012 at 17:33
  • @Andrew Just a friendly tip, you might want to think about getting some type of debugging method. Even die( var_dump( $variable ) ) works like a charm if you want to see what is being returned by some function. - Anyway glad it fixed your issue! :)
    – Jared
    Commented Feb 17, 2012 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.