Easy Content Types make it extremely easy to add new custom post types to your WordPress site. What is not always so easy is getting those post types to show up in your template files. This is mostly because a lot of themes do not support them very well. So I’m going to show you a few tricks to getting custom post types to show up in your template files the way you want them to.

Using WP_Query() in a Page Template

A lot of times you will want to show a list of custom post types on a particular page using a custom page template. When doing this, one of the easiest ways to show the post types, is to use WP_Query(). For this example, I’m going to assume that the custom post type list is being displayed in lieu of the regular page content. If you are simply including a list of post type entries at the bottom of your page content (or somewhere else), then consult Jeff Star’s article on 4 Ways to Loop WordPress.

So, to pull a list of custom post type entries in a page template, you can use query_posts(), like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');  
if ( get_query_var('page') ) $paged = get_query_var('page');
 
$query = new WP_Query( array( 'post_type' => 'books', 'paged' => $paged ) );
 
if ( $query->have_posts() ) : ?>
	<?php while ( $query->have_posts() ) : $query->the_post(); ?>	
		<div class="entry">
			<h2 class="title"><?php the_title(); ?></h2>
			<?php the_content(); ?>
		</div>
	<?php endwhile; wp_reset_postdata(); ?>
	<!-- show pagination here -->
<?php else : ?>
	<!-- show 404 error here -->
<?php endif; ?>
  • post_type – this is the post type you wish to display.
  • paged is required in order for pagination to work correctly
  1. oboston

    Thanks for the information… I just spent the past few hours trying to setup this type of query to display featured custom posts on my home page and this worked like a charm!

    • Pippin

      Excellent! Glad to help.

  2. Tony

    Hey Pippin,

    I’m learning so much from your tutorials.

    I’ve been searching forever for a way to have pagination with the Taxonomy terms. I will have a lot of terms, and I’m wondering if it’s something you’ve done, or if you can point me in the right direction.

    Everything I’ve found is for pagination of the actual posts, but nothing for the terms. My pagination is working great for the posts.

    Thank you!
    Tony

    • Pippin

      So you want to paginate the archive page for a particular term? Or do you want to paginate a list of terms inside of a taxonomy?

  3. Tony

    I’d like to paginate a list of terms inside a taxonomy. I found a plugin that does a great job, but it’s missing a few things i need, and I’d rather hardcode it into my theme.

    So it would be like this:

    CPT>Taxonomy:

    Term1 Term2 Term3 Term4 Term5

    • Pippin

      That gets pretty complex. How comfortable are you with your PHP?

  4. Tony

    i thought it might be. I guess i’m at an advanced beginner level 🙂 Usually I can find some code and make it work, but I’ve spent a couple days on this one and haven’t been able to do it.

    I’ve gotten the pagination working great for the actual taxonomy posts, just can’t get it for the terms list.

    adding to the complexity is that it’s in a loop that puts it into four columns, each with it’s own class and equal (adjusting) rows.

    But I’ve tried it without the columns and just couldn’t get it. I’ve gotten to paginate the entire loop of terms, but not a set number.

    Anyways, I don’t want to take up your time…so I’ll live with it for now and move on to more important things.

    Thank you!

    • jlucero

      can we at least see a query example or database string for something like this:


      in wordpress POST type
      ecpt tax: locations
      ecpt terms: usa, canada, mexico

      ecpt meta fields: paid, address, map

      i’m stuck within the Thesis theme and i’m trying to query those in a search bar, but i’m unfamiliar with your query terms. I’m also looking to put together a thesis guide on this. Thanks!

    • Pippin

      So you want to perform a query for all items in the default POST post type that are filed under the “usa”, “canada”, and “mexico” terms in the “locations” taxonomy? And then show the meta fields for each item? Is that right?

  5. mark shirley

    Hi how can I add taxonomy terms to this code:

    <a href="”>
    image<a href="”><img src="” alt=”text-here” />
    Product CodeID, ‘product_code’, true); ?>

  6. mark shirley

    <a href="">
    image<a href=""><img src="" alt="text-here" />
    Product CodeID, 'product_code', true); ?>

    • Pippin

      Mark, it looks like your code was stripped both times. Can you paste the code into snippi.com?

  7. jamie

    is there a way to display

    taxonomy name (as h1)

    taxonomy term
    post name
    post name

    taxonomy term
    post name
    post name

    etc..and have the post name link to the actual post? I’ve used the shortcode in a page and it’s listing out all the posts in the taxonomy just fine, but I’d like their individual terms to show above them. If I could do this in the template file and not as a shortcode that is fine. Thank you!

    • jamie

      I have this working well using a custom php code: http://pastebin.com/aQ09iT8i
      and it works beautifully. I just wondering what the process (if any) is for being able to do it through your plugin admin/process, as there are more options I’d like to add to this.

    • Pippin

      There is not an automated or short code way for doing this, but your code should work fine 🙂

  8. Peter

    Hey Pippin is there a way to add more paramters to extend the query to grab one one specific taxonomy term? ie call a post type, a taxonomy and one tax term?

    Ive tried &post_type=books&taxonomy=genre&tax_term=sci-fi&posts_per_page=-1&paged= but it just shows all the posts from the taxonomy and ignores the taxonomy term

  9. Brooke

    I’m trying to easily display my new custom post types from your awesome Easy Content Types plugin. I can’t find a shortcode that I can paste into my page WYSIWYG to display each custom post type along with the custom fields. Does that exist?

    • Pippin

      No, there is not a short code for that, sorry.

  10. Dave Navarro

    How do I access the meta box fields I added from PHP?

    For example, I create a post type of “job” and a metabox field called “city”. How do I access the “city”?

    • Dave Navarro

      Never mind, I figured it out. I used get_post_meta() to retrieve the meta box fields.

      My next problem is figuring out how to query items in a custom taxonomy.

      For example, all books in the genre “4” which is sci-fi.

  11. Edward

    My pagination does not work. my looks like this: 'product', 'posts_per_page' => 10, 'paged' => $paged ) );
    ?>
    have_posts() ) :
    while ($products->have_posts()) : $products->the_post(); ?>
    <div id="post-" >
    <a href="" style="color: #4a6572; text-decoration: none;">

    • Pippin

      Can you please post your code to snippi.com and then share the link?

  12. Mark Jackson

    Hi, newbee here…

    Love this pluggin, the back-end of setting up custom taxonomies is a breeze.
    The issue that I have is that WordPress templates or theme’s can never seem to register them within their default settings.

    Example: I purchased the Sahifa WordPress theme due to having a great frontpage builder that would be perfect for a site that I’m developing. However when I go to configure the frontpage in the theme’s frontpage builder – neither my taxonomies or custom post types show.
    Is there an easy/hard way of making this happen?

    Any answers would really be appreciated…

  13. Russell Aaron

    Pippin, first time caller, long time listener.

    I’d like to be able to only show one post.

    As of now, it shows as many posts as I have in the CPT. how can I only show the most recent one only?

    • Pippin

      Just set the ‘posts_per_page’ parameter to 1.

  14. Russell

    I’m trying to order posts by random, and everything I have search for does not worlk. Here is the snippit of code, can you please show me what I am doing wrong?

    $query = new WP_Query( array( ‘post_type’ => ‘homepagetestimonials’, ‘showposts’ => ‘1’, ‘orderby’ => ‘rand’, ) );

    • Pippin

      Use ‘posts_per_page’ => 1 instead of showposts. Does that work?

  15. Faisal

    $term->slug, ‘post_type’ => ‘designs’, ‘posts_per_page’ => 4, ‘orderby’ => ‘menu_order’ ) ); ?>
    have_posts() ) : $folio_loop->the_post();
    $fabric_name = get_post_meta(get_the_id(), ‘fabric_name’, true);
    ?>
    i above code i want display $fabric_name = get_post_meta(get_the_id(), ‘fabric_name’, true); in form.php that i select on click is shows exact that fabric name not random..please help

  16. Luis

    // Creates Recipes post type
    register_post_type(‘recipes’, array(
    ‘label’ => ‘Recipes’,
    ‘public’ => true,
    ‘show_ui’ => true,
    ‘capability_type’ => ‘post’,
    ‘hierarchical’ => false,
    ‘rewrite’ => array(‘slug’ => ‘recipes’),
    ‘query_var’ => true,
    ‘supports’ => array(
    ‘title’,
    ‘editor’,
    ‘excerpt’,
    ‘trackbacks’,
    ‘custom-fields’,
    ‘comments’,
    ‘revisions’,
    ‘thumbnail’,
    ‘author’,
    ‘page-attributes’,)
    ) );

    Add Your Post Type to a Custom Page Template

  17. Hemant Aggarwal

    How can I use ‘get_post_type()’ function to detect custom post types in ‘before_theme_setup’ hook ?

Comments are closed.