Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress From Bootstrap to WordPress Add Bootstrap Components to WordPress Theme Setting Up a Bootstrap Slideshow Carousel

I can't get the wp_query to work in this stage, even when just displaying the title.

<?php 
                $args = array(
                   'post_type' => 'post',
                   'category_name' => 'featured'
                );
                $the_query = new WP_Query($args);
            ?>

            <?php if (have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>
                <?php the_title(); ?>
            <?php endwhile; endif; ?>

2 Answers

Hi Mina,

Just as a heads up I edited your post just to fix up some of the code embedding, it wasn't completely displayed within a code block, also if you use the language after the first 3 ticks, in this case php, it will help with syntax highlighting.

You are very close! You are just missing the query variable before the initial have_posts() function:

<?php 
$args = array(
    'post_type' => 'post',
    'category_name' => 'featured'
);
$the_query = new WP_Query($args);
?>

<?php if ($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; ?>

Give that a try and that should take care of it.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,689 Points

I had this exact same issue and I thought I was going to have to write a new forum post about it.

Then I looked at my category name.

I too had it set to featured but the actual name that of the category that I had was features.

I expect by now you got the problem sorted but I was able to look at my categories and compare the 2. The titles will now show up and I'm now up to date. So just make sure your category names match up in Admin area and in your code.

<?php 
$args = array(
    'post_type' => 'post',
    'category_name' => 'features'
);
$the_query = new WP_Query($args);
?>