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

Dan Ridley
PLUS
Dan Ridley
Courses Plus Student 14,839 Points

Requiring 2 specific categories to match to display post

Hello everyone,

I need some help with the WordPress issue. I coded a section of my site that I only want posts that match to specific categories at the same time to display in this area. The categories will be "events" and "featured".
I have watch the videos on how to make a WordPress theme and it only specifies how to display post that match one specific category. I need to match to specific categories at the same time.

Any help would be greatly appreciated and if you have any questions please let me know.

Dan

5 Answers

Here is an idea:

Within your $args array, try using

'tax_query' => array(
    array (
    'taxonomy' => 'category',
    'terms' => array('events'),
    'field' => 'slug'
    ),
   array ( 
  'taxonomy' => 'category',
  'terms' => array('featured'),
  'field' => 'slug'
   )
)

The loop will only return posts that have both the 'events' and 'featured' categories assigned to them. If you put both events and featured within a single term array (i.e., 'terms' => array('events','featured')), this would be treated like an "OR" statement: return any post that has either an events OR featured category assigned to it.

Calvin Nix
Calvin Nix
43,828 Points

Hey Dan,

Do you mind providing the code that you currently have in place?

Thanks, Calvin

Dan Ridley
PLUS
Dan Ridley
Courses Plus Student 14,839 Points

Sure, no problem. Thanks for responding.

                   ```
                   <?php
                            $args = array(
                                'post_type' => 'pricing',
                                'category_name' => 'weddings',
                                'posts_per_page' => 1
                            );

                            $the_query = new WP_Query( $args );
                        ?>

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

                      <div class="col-xs-12 col-sm-4 col-md-4">
                        <h3>Weddings</h3>
                            <a href="<?php bloginfo('url'); ?>/pricing/" class="thumbnail">
                              <div>
                                <div class="price">
                                    <p style="color:black">Starting From</p>
                                    <h3>$<?php the_field('price'); ?></h3>
                                </div>
                                <div class="investment-description">
                                    <p><?php the_field('hours'); ?> Hour Session</p>
                                    <hr class="half-rule">
                                    <p><?php the_field('photographers'); ?></p>
                                    <hr class="half-rule">
                                    <p><?php the_field('flash_drive'); ?> flash Drive</p>
                                </div>
                              </div>
                            </a>
                          </div>

                          <?php endwhile; endif; ?>
                    ```
Dan Ridley
PLUS
Dan Ridley
Courses Plus Student 14,839 Points

Hey Chris,

It worked! Thanks for you help. Not sure how you did it but thanks. I will have to go through the code line by line and see if i can understand

Hello Dan--

I'm glad it worked. If it helps, I'll provide a little more information on the code provided.

First of all, since categories and tags are just taxonomies, you can use the tax_query function to make more specific queries within the database as you run through the loop.

It is a little weird looking because it takes an array of arrays as its value.

<?php
'tax_query' => array(  array ($taxonomy, $field, $term ), array ($taxonomy, $field, $term )   )

Each inner array represents the a term or terms that you would like to have added to your query arguments. There are five parameters that you can pass within these inner arrays--three of which I used in the code above.

First, you identify the name of the taxonomy which contains the terms you want to check as the database finds relevant posts. Since you want to query categories--one of WP's default taxonomies--you would enter 'category' as the taxonomy name. If you wanted to check tags, you would enter 'tag' as the taxonomy name. And if you used a custom taxonomy, you would use that name.

Then, you need to identify which term or terms from that taxonomy you want. This can be either a slug or the term id. Since there can be multiple term values you can add here, you use another array here.

I do believe that if you are passing a single value, you don't need the array you can just pass the string, e.g., 'terms' => 'events'...but I'm not sure.

Since you have the option to pass either the slug or id of the term, you need to identify what type of value Wordpress should expect. That is where the field comes in. If the value of terms was an id (or ids), you don't need to add the 'field' option, since 'term_id' is the default value. However, since you wanted to find posts by a slug (e.g., 'events', 'featured'), you would want to identify that the terms are slugs and not ids.

<?php
'tax_query' => array(  
    array ( 
       'taxonomy' => 'category',
       'terms' => array ('events'),
       'field' => 'slug'
      ),
  )

When you pass multiple terms into the terms array, Wordpress by default identifies any posts that have at least one of those terms assigned to it. In your case, where you wanted only posts that were assigned both of your terms, it made sense to add a second inner array for the 'featured' category. This way, Wordpress treats the terms as an 'AND' operation during the query.

I hope this makes better sense of the approach I took. Please note I'm still learning so there might be a more efficient way, but this was how I was able to get this working.

Dan Ridley
PLUS
Dan Ridley
Courses Plus Student 14,839 Points

Hey Chris,

First of all thanks so much for taking the time to write that out for me. It was helpful. I wouldn't have even noticed that you were still learning! You seem to have picked it up real well.

I am becoming a huge fan of Wordpress. if you ever want to chat about it let me know!

Dan