All posts are still shown when adding category argument to query

I’ve got the following code in my team-type.php file which I’m using to create a custom taxonomy for a member information:

function create_team_taxonomies() {
    $labels = array(
        'name'              => _x( 'Kategooriad', 'taxonomy general name' ),
        'singular_name'     => _x( 'Kategooria', 'taxonomy singular name' ),
        'search_items'      => __( 'Otsi kategooriatest' ),
        'all_items'         => __( 'Kõik kategooriad' ),
        'parent_item'       => __( 'Vanem kategooria' ),
        'parent_item_colon' => __( 'Vanem kategooria:' ),
        'edit_item'         => __( 'Muuda kategooriat' ),
        'update_item'       => __( 'Uuenda kategooriat' ),
        'add_new_item'      => __( 'Lisa uus kategooria' ),
        'new_item_name'     => __( 'Uus kategooria nimi' ),
        'menu_name'         => __( 'Kategooriad' ),
    );

    $args = array(
        'hierarchical'      => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'categories' ),
    );

    register_taxonomy( 'team_categories', array( 'team' ), $args );
}
add_action( 'init', 'create_team_taxonomies', 0 );

But when I call the query in page-team.php the following way:

<?php query_posts( 'post_type=team&cat=noukogu&showposts=15' ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php $title=s tr_ireplace( '"', '', trim(get_the_title())); $desc=s tr_ireplace( '"', '', trim(get_the_content())); ?>

<div class="member">

    <img class="image" src="https://wordpress.stackexchange.com/questions/159034/<?php print team_thumbnail_url($post->ID) ?>" alt="liikme pilt">

    <h3 class="title"><?php echo $title; ?></h3>
    <p class="description">
        <?php echo $desc; ?>
    </p>

</div>
<!-- End member -->

<?php endwhile; endif; ?>

All of the member posts show up. Not the noukogu category posts.

Any ideas what might be causing this issue?

UPDATE: I understand now that cat is used for category ID, but when I change it to category_name(to use category slug) no posts show up at all.

2 Answers
2

First: don’t use query_post for secondary loops. It alter the main query and you can end up with wire results. Instead use a new WP_Query instance. See this question for detail info.

Now with your proble. You are using the “cat” parameter and that is for “category” taxonomy, not for your custom taxonomy. You should use the tax_query parameter instead:

<?php
$tax_query =  array(
                 'taxonomy' => 'team_categories',
                 'field'    => 'slug',
                 'terms'    => 'noukogu'
              );

$args = array(
    'post_type' => 'team',
    'tax_query' => array( $tax_query ),
 );

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();

        $title = str_ireplace( '"', '', trim(get_the_title())); $desc=str_ireplace( '"', '', trim(get_the_content()));
        ?>

        <div class="member">
          <img class="image" src="https://wordpress.stackexchange.com/questions/159034/<?php print team_thumbnail_url($post->ID) ?>" alt="liikme pilt">
           <h3 class="title"><?php echo $title; ?></h3>
            <p class="description">
                <?php echo $desc; ?>
            </p>

      </div>
      <!-- End member -->

   <?php
   }
}

wp_reset_postdata();
?>

Leave a Comment