Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress The WordPress Template Hierarchy Custom Post Type Templates in WordPress Custom Post Type Templates Continued

Fernando Cortes
Fernando Cortes
2,435 Points

How to display custom posts on home, blog and archives page?

I'm doing a test on a local install of my website, using a child-theme of a theme I purchased. Using Custom Post Types UI, I created a post type called "altpost", and a single-altpost.php template to be applied. I've confirmed that this works by adding the word "text" to the single-altpost.php file.

Now, I notice that the test post I published using this custom type does not appear in the home page of my site (set to static page) or in the blog and archive pages of my site.

I had a look at the Codex and found a section called "Custom Post Types in the Main Query" which I thought might be relevant here, so I added this to the functions.php file of my child theme:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
  if ( is_home() && $query->is_main_query() )
    $query->set( 'post_type', 'altpost' );
  return $query;
}

But the custom test post still won't display on either page (home, blog or archives). I tried changing is_home for is_archives, just to see if the post would at least show up on the archives page, but it still didn't work.

What am I doing wrong?

2 Answers

Nattapol Kamolbal
Nattapol Kamolbal
15,528 Points

You can copy front-page.php or home.php (or maybe other) from your theme and edit some line of it in your child theme. For more detail about which files you should copy please see: the codex or wphierarchy.

From then you can specify the post type you want to display with WP_Query. The simplified code will look something like this:

$args = array(
    'post_type' => 'altpost',
);

$the_query = new WP_Query($args);

if($the_query->have_posts()): while($query->have_posts()):$query->the_post();

This method has more controllable than using the hook which effect all of the site.

I also recommend you to read about the loop or taking WordPress Theme Development course.

Fernando Cortes
Fernando Cortes
2,435 Points

Thanks Nattapol, it hadn't occurred to me that I could also do this by using modified page templates in the child theme. Several ways to skin a cat, as they say.

I'm guessing that Stanley's suggestion would make sense if I wanted the altposts to be treated as any other post across the whole site, while yours would allow for finer control on specific pages. I will try both and see for myself.

As I mentioned above, I really need to give a couple of in-depth readings to the articles about the loop, WP_Query, etc. I just started the WP Theme Development Course, so that should help me understand how things work.

Stanley Thijssen
Stanley Thijssen
22,831 Points

Hi Fernando,

A good thing for you is read the following page:

https://codex.wordpress.org/Class_Reference/WP_Query

This explains the WP_Query Object. You can create your own query with this object and display any custom post you like.

Fernando Cortes
Fernando Cortes
2,435 Points

Thanks Stanley. I see there's a whole section about post types in the WP_Query article that I wasn't aware of. It's going to take a few careful readings of that article to actually comprehend it.