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

Liam Maclachlan
Liam Maclachlan
22,805 Points

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

Liam Maclachlan
Liam Maclachlan
22,805 Points

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

<?php $args = array('post_type' => 'portfolio'); ?>
Konrad Pilch
Konrad Pilch
2,435 Points

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

Liam Maclachlan
Liam Maclachlan
22,805 Points

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

Konrad Pilch
Konrad Pilch
2,435 Points

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.

Liam Maclachlan
Liam Maclachlan
22,805 Points

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).

Konrad Pilch
Konrad Pilch
2,435 Points

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