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

Why does my custom Query not return the chossen post type only?

Any ideas? It is returning all standard posts from WordPress and my custom Post type, 'portfolio', is just being ignored... What is going wrong? 0_o

    <?php if( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
    <?php endwhile; endif; ?>

    <?php $args = array('post_type', 'portfolio'); ?>
    <?php $query = new WP_query($args); ?>
    <?php if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="entry__container cf">
            <div class="blog__featured-image--archive">
            </div>
            <h2 class="entry__mainheader"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
        </div>
    <?php endwhile; endif; ?>

1 Answer

I'm an idiot. I was using a comma in the array. Just noticed the error. Fixed now :)

<?php $args = array('post_type' => 'portfolio'); ?>

Your not an idiot, just learning and gaining experience :)

Love how on every post you kinda reply to your self xD

Put your self best answer :D

Haha! I never knew I had a reputation :p I find 'talking' out loud is a good way to get a different perspective on a problem

Yes, me too! Whenever i write the problem i have, an idea comes up! So i can make like 10 posts, and while writing them i can get 5 of them the good answer.

Yeah. I feel bad putting 'best answer' on them, so I wait for the email, if no one else has put one on it or improves it (obviously).

Well, if i put my self best answer im happy because i solved it : p but its weird a lil.