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

How to use WP_Query showing multiple category from custom_post_type? Need urgent help!

How do i show only the image and title from projects(custom_post_type) based on multiple category? ie: upcomming, ongoing, sold, and each of them will also be in residential, commercial and mixed category in a project page?. I also need to make them filterable. any idea please...

<?php
        // Start the loop.
        while ( have_posts() ) : the_post();

            // Include the page content template.
            get_template_part( 'content', 'page' );

        // End the loop.
        endwhile;
        ?>

        <?php 
        // Wp query starting

        wp_reset_postdata();

            $args = array(
                'post_type'= 'project'
//multiple category needed
                'category_name' => 'upcomming',   
                'post_per_page' => 10
                );

            $the_query = new WP_Query( $args );

        // wp_reset_postdata();

        ?>

            <?php
        // Start the loop with query.
        while ($the_query->have_posts() ) : $the_query->the_post();

            // Include the page content template.
            get_template_part( 'content', 'page' );

            // End the loop.
        endwhile;
        ?>

1 Answer

If you need multiple categories, you can add them within the string you assign to the category name.

<?php
$args = array(
                'post_type'= 'project'
//multiple category needed
                'category_name' => 'upcoming,residential'   
                'post_per_page' => 10
                );

Keep in mind that this will treat this as an OR relationship: 'find me any articles that have been tagged either as upcoming OR residential'.

If you want an AND relationship "find me any articles that have been tagged both as upcoming AND residential", then you will need to use a + instead of a comma to separate the terms.

<?php
$args = array(
                'post_type'= 'project'
//multiple category needed
                'category_name' => 'upcoming+residential'   
                'post_per_page' => 10
                );

There is an important limitation to point out. You can't mix both OR and AND relationships using 'category_name'. In other words, you won't be able to say: 'find me articles that are either upcoming or ongoing and also are residential'.

Now if you need some terms to be OR relationships and other terms to be AND relationships, then you will need to look into setting up a custom taxonomy...which is probably better suited for what you are trying to accomplish. In that case, you can use a tax_query to identify exactly the terms that articles need to be filtered by.

As a side note, it may just be what you copied into the example above, but look at your usage of wp_reset_postdata(). It should be called AFTER you run through your custom query.

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

            // Include the page content template.
            get_template_part( 'content', 'page' );

            // End the loop.
endwhile;

wp_reset_postdata();