3

I am working on Plugin development and my plugin name is plugindev.I have a custom post type called team.I have a custom taxonomy Team_Category which is being registered by this code

/***************************taxonomy****************************/
add_action( 'init', 'create_team_taxonomies', 0 );
function create_team_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Team_Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Team_Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Team_Categories' ),
        'all_items'         => __( 'All Team_Categories' ),
        'parent_item'       => __( 'Parent Team_Category' ),
        'parent_item_colon' => __( 'Parent Team_Category:' ),
        'edit_item'         => __( 'Edit Team_Category' ),
        'update_item'       => __( 'Update Team_Category' ),
        'add_new_item'      => __( 'Add New Team_Category' ),
        'new_item_name'     => __( 'New Team_Category Name' ),
        'menu_name'         => __( 'Team_Category' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => false,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'Team_Category' ),
    );

    register_taxonomy( 'Team_Category', array( 'team' ), $args );
}
/****************************taxanomy end***********************************/

But when I use a tax_query in my WP_Query, I do not get any posts. here is my code

<?php
$taxonomy_name = 'Team_Category';
$get_categories = get_terms($taxonomy_name);
$total_categories = count($get_categories);
// Loop through the obituaries:
for ($i = 0; $i < $total_categories; $i++) {
    ?>
    <div class="row">
        <div class="col-md-4">
            <?php echo $category_name = $get_categories[$i]->name; ?>
        </div>
        <?php
        $args = array(
            'post_type' => 'team',
            'tax_query' => array(
                array(
                    'taxonomy' => 'Team_Category', 
                    'field' => 'slug', 'terms' => $category_name,)
                )
         );

        $query = new WP_Query($args);

        if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            the_title();
        }
        }
        wp_reset_query(); ?>

    </div>

<?php }

It works perfectly without tax_query.I did lot of google but found no suitable result.Any solution to solve this problem .Any help would be highly appreciated

5
  • What happens if you wrap your $category_name in an array()? Like so: 'terms' => array( $category_name ).
    – Howdy_McGee
    Jan 29, 2016 at 18:27
  • @Howdy_McGee Nothing happens still same situation...
    – raxa
    Jan 29, 2016 at 18:32
  • it seems like the code is fine. I copied / pasted it and I didn't have a problem with it. Are you certain that the category has posts attached to it?
    – Howdy_McGee
    Jan 29, 2016 at 18:43
  • @Howdy_McGee as i have pasted this question in stack overflow where I got its solution by using 'taxonomy' => 'team_category','terms' => $category_name, and changing Team_Category to team_category.. but I don't know why this code is not running 'tax_query' => array(array( 'taxonomy' => 'team_category', 'field' => 'slug', 'terms' =>$category_name)) even i have changed Team_Category to team_category
    – raxa
    Jan 29, 2016 at 18:59
  • I used the capitalization and didn't run into an issue. Maybe break your code into bits to ensure you're getting the expected data. print_r( $get_categories ); before the loop to ensure your terms are in there. Mayb remove your WP_Query from the for loop and pass in a string slug that you know has posts assigned to it to ensure your WP_Query works.
    – Howdy_McGee
    Jan 29, 2016 at 19:01

1 Answer 1

0

I think that the problem is right in your code, but it's really easy to overlook. Let's take a closer look at this part:

    <div class="col-md-4">
        <?php echo $category_name = $get_categories[$i]->name; ?>
    </div>
    <?php
    $args = array(
        'post_type' => 'team',
        'tax_query' => array(
            array(
                'taxonomy' => 'Team_Category', 
                'field' => 'slug', 'terms' => $category_name
            )
        )
    );

What exactly is going on in here? You get name of term and store it as $category_name ($category_name = $get_categories[$i]->name;).

Later, you use this value as a slug ('field' => 'slug', 'terms' => $category_name).

Slug and name are different things, so there is a really big probability that your Tax_Query won't find anything (if name contains capital letter, space or any special character, slug will be different from name).

So it should work fine, if you'll change it so it looks like this:

    <div class="col-md-4">
        <?php echo $get_categories[$i]->name; ?>
    </div>
    <?php
    $args = array(
        'post_type' => 'team',
        'tax_query' => array(
            array(
                'taxonomy' => 'Team_Category', 
                'field' => 'slug',
                'terms' => $get_categories[$i]->slug;
            )
        )
    );

    $query = new WP_Query($args);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.