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

Jacob Roman
Jacob Roman
22,640 Points

Wordpress WP_Query not listing posts by categories.

I am trying to display posts from a certain category from a Custom Post Type that I created with the Custom Post Type UI plugin. It seems like the Taxonmy is not connected to the post type some how. But i have connected it and made sure that they show for the post type but I cant get this code to display the post from the specific category on the front-end. Any suggestions?

<section class="cd-faq">

<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'faqs_categories';
$tax_terms = get_terms($taxonomy);
?>
<ul class="cd-faq-categories">
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="#' . $tax_term->slug . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?><?php wp_reset_query(); ?><?php wp_reset_postdata(); ?>
</ul>

<div class="cd-faq-items">
    <ul id="cylinder-head-selection-guide" class="cd-faq-group">
        <li class="cd-faq-title"><h2>Cylinder Head Selection Guide</h2></li>


        <?php 

        $args = array(
            'post_type' => 'faqs',
            'posts_per_page' => -1,
            'cat' => 838
        );

        // the query
        $the_query = new WP_Query( $args ); ?>

        <?php if ( $the_query->have_posts() ) : ?>

            <!-- pagination here -->

            <!-- the loop -->
            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <li>
                    <a class="cd-faq-trigger" href="#0"><?php the_field('question'); ?></a>
                    <div class="cd-faq-content">
                        <p><?php the_field('answer'); ?></p>
                    </div> <!-- cd-faq-content -->
                </li>
            <?php endwhile; ?>
            <!-- end of the loop -->

            <!-- pagination here -->

            <?php wp_reset_postdata(); ?>

        <?php else : ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; ?>

1 Answer

Jacob Roman
Jacob Roman
22,640 Points

Well that was quick. In case anybody else has trouble with this. You have to set up the query $args a little different.

Change:

   $args = array(
        'post_type' => 'faqs',
        'posts_per_page' => -1,
        'cat' => 838
    );

To this:

        $args = array(
            'post_type' => 'faqs',
            'posts_per_page' => -1,
            'faqs_categories' => 'cylinder-head-selection-guide'
        );

Hope this helps someone else.