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

Ben Silvertown
Ben Silvertown
987 Points

WP_Query pulling wrong post types?

I've used Custom Post Types and Custom fields to create some portfolio posts but when I try and call them in a loop WP pulls posts from my default "Posts" section rather than my "Portfolio" section.

Here's my code:

<?php

                        $args = array(
                            'post-type' => 'portfolio'
                            );
                        $query = new WP_Query( $args );

                    ?>

                    <div class="caption-container">

                        <?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>

                            <div class="projectcaptions hide-mobile">
                                <div class="worthdesc">
                                    <?php the_post_thumbnail('large'); ?>
                                    <h3><?php the_title(); ?></h3>
                                    <p class="fields"><?php the_field('fields'); ?></p>
                                    <p class="captions"><?php the_field('the_brief'); ?></p>
                                </div>
                            </div>

                        <?php endwhile; endif; wp_reset_postdata(); ?>

                    <div>

I've double checked to make sure my CPT slug matches the post type query (which it does) but I just can't seem to figure out why its ignoring that array?

1 Answer

Ben Silvertown
Ben Silvertown
987 Points

Just figured out my syntax was off!

'post-type' should be 'post_type'

Fixed and its now working! Just thought i'd post incase anyone else had this problem...