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

custom post categories

I have added custom posts 'Webcasts' to my site, displaying only the first one on the homepage, code below:

<?php $num_posts = ( is_front_page() ) ? 1 : -1;

$args = array(
'post_type' => 'webcasts', 
'posts_per_page' => $num_posts
);

$query = new WP_Query( $args );

?>

I have created 3 categories for this post type (next webcast, previous webcast, and upcoming webcasts). On this page, I only want to display one of these categories, how do I do this??

5 Answers

In case this helps anyone else I have figured out the problem, correct code is:

$args = array( 'post_type' => 'meetings', 'category_name' => 'next-meeting', );

So happy right now!!! :-)

Chris McKirgan
Chris McKirgan
5,666 Points

This may be of some use to you: https://css-tricks.com/snippets/wordpress/run-loop-on-posts-of-specific-category/

$args = 'cat=5'; // ID of the category
$query = new WP_Query( $args );

ok, I've changed it to this but now it won't display any posts... ?

$num_posts = ( is_front_page() ) ? 1 : -1;

                $args = array(
                    'post_type' => 'webcasts', 
                    'posts_per_page' => $num_posts
                );
                $args = 'cat=next-webcast'; 
                $query = new WP_Query( $args );

I've also tried this which seems like it should work, but it just displays the first post, not the specific category :/

$args = array( 'post_type' => 'webcasts', 'cat' => 'next-webcast', 'posts_per_page' => $num_posts );

I'm still stuck on this, please can anyone help???