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 WordPress Theme Development Custom Post Type Templates in WordPress The Portfolio Homepage

Matt Jakachira
seal-mask
.a{fill-rule:evenodd;}techdegree
Matt Jakachira
Front End Web Development Techdegree Student 5,536 Points

After adding the WP_Query my page just spins?

Here is my code, I made a change to WP_Query as I wanted to pull posts that had a specific category....

<!--Used to display Posts that match the category DryPeel-->

    <?php
     $query = new WP_Query( array( 'category_name' => 'DryPeel' ) );

    ?>

    <div class="like-content-posts">

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

          <div class="like-posts">
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium'); ?></a>
          </div>

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

    </div>
Peter Gregory
Peter Gregory
6,614 Points

I did the same thing. My site was just displaying posts, over and over again. Here's what I did to fix the repeating post images:

Wrong : $query->the_posts(); />

Right: $query->the_post(); />

2 Answers

Sue Dough
Sue Dough
35,800 Points

You can't do it like that. You need to do a tax query. This should get you going as long as you can figure out the proper custom post type name, taxonomy name, and the lowercase slug category name.

<?php
  // Category Query
  $args = array(
    'post_type' => 'custom_postype_name', // YOU NEED TO CHANGE THIS
    'tax_query' => array(
      array(
        'taxonomy' => 'customtaxonomy_category', // YOU NEED TO CHANGE THIS TO YOUR TAXONOMY
        'field'    => 'slug', 
        'terms'    => 'drypeel', // THIS HAS TO BE LOWERCASE, CHECK THE SLUG COLUMN IN BACKEND
      )
    )
  );
  $category_query = new WP_Query( $args );
?>

<div class="like-content-posts">

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

    <div class="like-posts">
      <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'medium' ); ?></a>
    </div>

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

</div>
Matt Jakachira
seal-mask
.a{fill-rule:evenodd;}techdegree
Matt Jakachira
Front End Web Development Techdegree Student 5,536 Points

So i did change the arg$ info according to my custom post type and taxonomy but nothing shows on the page

<?php // Category Query $args = array( 'post_type' => 'markets-subcontent', // YOU NEED TO CHANGE THIS 'tax_query' => array( array( 'taxonomy' => 'market', // YOU NEED TO CHANGE THIS TO YOUR TAXONOMY 'field' => 'Market', 'terms' => 'drypeel', // THIS HAS TO BE LOWERCASE, CHECK THE SLUG COLUMN IN BACKEND ) ) ); $category_query = new WP_Query( $args ); ?>

How do i go about troubleshooting this?

Sue Dough
Sue Dough
35,800 Points

Matt Jakachira make sure you do

'field'    => 'slug', 

you put 'Market' there which won't work. So change that and if it still does not work then double check your taxonomy name.