1

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="<?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.

1
  • Have you read any of the answers? cat is for "category" taxonomy, either ID or name won't work for you because you are using other taxonomy called "team_categories".
    – cybmeta
    Commented Aug 25, 2014 at 12:40

2 Answers 2

3

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="<?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();
?>
6
  • Ty for the help. Although getting a parse error unexpected '>' after $query->the_post();.
    – Laniakea
    Commented Aug 25, 2014 at 12:39
  • I've just wrote here, not tested the code. Try again.
    – cybmeta
    Commented Aug 25, 2014 at 12:42
  • Parse error: syntax error, unexpected 'tr_ireplace' (T_STRING)
    – Laniakea
    Commented Aug 25, 2014 at 12:43
  • That function name is from the code you posted. See s tr_ireplace in your code. I replaced with str_ireplace which is the correct function name. So, I think you are not testing the code I posted.
    – cybmeta
    Commented Aug 25, 2014 at 12:45
  • 1
    +1 posted within the same timeline :-). Your's is a bit more complete though Commented Aug 25, 2014 at 12:54
3

You have a few flaws in your code that are causing this failure

First of all, and most important of all, never (my emphasis) use query_posts to construct a custom query.

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)

Rather use a class like WP_Query to construct custom queries

As for the reason your query fails when using the category parameter, it is quite obvious that you are not making use of categories, or actually category terms :-). You are making use of a custom taxonomy with terms, which does not work with the category parameters in WP_Query. You should be making use of the tax parameters (tax_query) which you can go and check out in this link.

Example

$args = array(
    'post_type' => 'team',
    'tax_query' => array(
        array(
            'taxonomy' => 'team_categories',
            'field'    => 'slug',
            'terms'    => 'SLUG FOR PARTICULAR TERM',
        ),
    ),
);
$query = new WP_Query( $args );

This link will also provide details on how to construct a proper query using WP_Query

For the difference between a category and a custom taxonomy, go and read my answer here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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