Search code examples
phpwordpresstaxonomy

Query custom taxonomy wordpress error


I have a custom query which I'm trying to run. How I want it to work is,

  • Firstly, check the page title

  • if the page title is the same as the name of the cat from a custom taxonomy then show all the posts that are in that custom taxonomy - post type.

The only trouble is, it isn't returning anything, I've made sure I'm targeting the correct 'post type' & correct 'taxonomy' name. It will return the cat id with the following:

$cat->cat_ID;

But won't return any posts, here is my code:

<?php 

    // Get the name of the page
    $theTitle = get_the_title();
    //Get the taxonomy for the custom post type 
    $categoryselect = array( 'taxonomy' => 'team-members' );

    $categories = get_categories($categoryselect);

    // loop through each category as cat
    foreach ($categories as $cat):

        //If cat is the same as title *name* then lets do something     
        if($theTitle == $cat->cat_name):?>
            <h3>We’re here to help.</h3>            
        <?php   

            $catID = $cat->cat_ID;
            //echo $catID;

            //query the posts but, use the cat ID so the page relates to it.
            $args = query_posts(array( 'post_type' => 'team', 'cat'=> $catID, 'orderby' => 'title', 'showposts' => -1 ));
            $loop = new WP_Query( $args );

            // run the loop for posts
            while ( $loop->have_posts() ) : $loop->the_post();?>
            <div class="person">
                <h5><?php the_title(); ?></h5>
            </div>
            <?php endwhile; 

        endif; 

    endforeach;

This is on a page.php template

Any suggestions?


Solution

  • You have a couple of issues here. Lets start with this line

    $args = query_posts(array( 'post_type' => 'team', 'cat'=> $catID, 'orderby' => 'title', 'showposts' => -1 ));
    
    1. query_posts should never be used, and nor should you make use of two queries in one

    Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

    1. Secondly showposts has been depreciated in favor of posts_per_page

    2. You terminology is incorrect, and therefore you are making use of wrong parameters in your query. You are not working with categories here, but with a custom taxonomy and terms. To get a good perspective of categories, terms and custom taxonomies, see this post I have done on WPSE

    You should make use of a tax_query and not the category parameters in WP_Query

    1. To come back to how you get your terms. The way you are doing it with get_categories() is not wrong, but it can become confusing as you are actually working with a custom taxonomy and not the build-in category taxonomy. I would suggest to use get_terms() instead

    2. I actually feel that you don't need to use get_terms, get_categories or the foreach loop. I have checked your code and it seems the only time that something will show is when the term name is equal to the page name.

    You already have the taxonomy name and the term name, the only thing that you can maybe do is to check if the term exists, and then feed that to your custom query

    This is a modified version of your code, UNTESTED

    <?php 
        // Get the name of the page
        $theTitle = get_the_title();
        //Get the taxonomy for the custom post type 
        $taxonomy = 'team-members';
        //Set the page title as term name 
        $term = $theTitle;
    
            if( term_exists( $term, $taxonomy ) ) : // Check if there is a term that match the page title ?>
    
                <h3>We’re here to help.</h3>            
            <?php   
    
                //query the posts but, use the cat ID so the page relates to it.
                $args = array( 
                    'post_type' => 'team', 
                    'orderby' => 'title', 
                    'posts_per_page' => -1,
                    'tax_query' => array(
                        array(
                           'taxonomy'           => $taxonomy,
                           'field'              => 'name',
                           'terms'              => $term,
                           'include_children'   => false
                        ),
                    ),  
                );
    
                $loop = new WP_Query( $args );
    
                // run the loop for posts
                while ( $loop->have_posts() ) : $loop->the_post();?>
                <div class="person">
                    <h5><?php the_title(); ?></h5>
                </div>
                <?php endwhile; 
    
            endif; 
    ?>
    

    EDIT

    Have now tested the code and made a few minor adjustments. It is working 100% now on my local install.