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


Steve Linn
Steve Linn
11,841 Points

Help with WP_Query

I need a little help with the construct of this WP_Query Loop please

All my posts are custom post types with the name "neighborhoods"

I want to display the content for the custom post if it has a the category of "in town" ---display all these posts

if category is "out town" -- display all these posts

so how do I write this out "if category is = in town -----display the posts with that category

if category is = out town then display the posts with the out town category

  $args = array(
      'post_type' => 'neighborhoods',

$the_query = new WP_Query($args);


     if ( have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();?>

<!--content area -->
  <div class="row-fluid">
    <h3><a href="<?php the_permalink();?>"><?php the_title() ;?></a></h3>
<!--closes content area-->
<?php endwhile; else: ?>
<p>No posts</p>

<?php endif; ?> 

<?php wp_reset_postdata(); ?>   

4 Answers

Julian Price
Julian Price
11,760 Points

I should mastermind with you because I am trying to create something similar. So my question would be if register the post type = neighborhoods did you also create/register support for taxonomy on neighborhood catorgorys.

I am just still tying to learn/wrap my head around php but I believe you missing an array for have post with 'neighborhoods'.

I am not sure though.

Steve Linn
Steve Linn
11,841 Points

neighborhoods is not a category but a custom post type named "neighborhoods"

I used the custom fields plug in to develop this

the loop works -- in that it displays all the correct information for all the custom post type neighborhoods --- I just need it to filter by category now

I think it's actually not in this page that I need to be concerned with but the page I am coming from ---- so, when someone clicks the link that says "in town" it displays the results….and vice versa

Julian Price
Julian Price
11,760 Points

But you wanting a taxonomy of cat/tags that would be intown

Post Types = Neighborhoods ( Singe-Neighborhood Page) Taxonomy: Cats or Tags = Intown than you need a archive-neighborhoods

I found this video session that Aaron Holbrook did on Wordsesh it all finally to begin to make sense:


also check out Justin Tablock Post on http://justintadlock.com/archives/2013/10/07/post-relationships-parent-to-child

Matt Campbell
Matt Campbell
9,767 Points


Codex is your friend. Point 5.2.

You'll basically be running two loops. One for one category, one for the other.

Steve Linn
Steve Linn
11,841 Points

Thanks Matthew, How do I actually pass it from one page to another?

On my page "neighborhoods" I want to set up 2 links - one that goes to neighborhoods in town and one that goes to neighborhoods out of town (really the same page with different query results)….

how do I set up the links on the neighborhoods page and pass the category_name to the results page?