WordPress.org

Support

Support » How-To and Troubleshooting » Custom taxonomy and query_posts

Custom taxonomy and query_posts

  • Hi,
    I’m playing with custom taxonomies – and would like to query posts which have a taxonomy value assigned to them… But it seems WordPress doesn’t take care of the condition and returns all posts.

    The way I have defined my taxonomy:

    register_taxonomy( ‘officialAlbums’, ‘album’, array( ‘hierarchical’ => false, ‘label’ => ‘Official Albums’, ‘query_var’ => true, ‘rewrite’ => true ) );

    The way I’m querying WordPress:

    query_posts( array( ‘post_type’ => ‘album’, ‘officialAlbums’ => ‘xxx’, ‘posts_per_page’ => 10 ) );

    Even if I have two articles tagged with ‘officialAlbums=xxx’, WordPress ignores the ‘officialAlbums’ parameter.

    Could you figure what I’m doing wrong?

    Thanks,
    Nicolas

Viewing 15 replies - 1 through 15 (of 22 total)
  • I know that there was a bug like what you describe and that is fixed in the coming Version 3.0. I just don’t know if the bug existed at 2.9.2.

    Thanks Michael ! I’m currently using 3.0beta2, and hopefully it’s a bug in the beta that will be fixed in the final one…

    With a post_type of book, and a taxonomy of genre, this returns books where the genre is mystery:

    <?php
    $args=array(
      'genre' => 'mystery',
      'post_type' => 'book',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Books with genre of mystery';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    MichaelH, thanks for the code above, you saved me so much work.

    One question, what if you had two taxonomies, ‘genre’ and ‘publisher’ for the post type ‘book’. I tried the following query, but it didn’t work:

    <?php
    $args=array(
      'genre' => 'mystery,random-house',
      'post_type' => 'book',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Books with genre of mystery';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Hi everybody! In the Code which MichaelH we decide to show jsut the posts which are in the “genre” (or taxonomy) which is called “mystery”. How can we show ALL the posts which are NOT with the genre “mystery” ??? Please help me!!! Thank you so much

    Just in case somebody is reading here…there is another threat which deals more exactly with this question:

    http://wordpress.org/support/topic/exclude-special-taxonomy-from-a-custom-post-type?replies=3

    alanchrishughes

    @alanchrishughes

    @leroy12

    Did you ever figure this out? I’m having the same problem, it just returns everything from that post type and ignores the taxonomy.

    alanchrishughes

    @alanchrishughes

    Anybody? What MichaelH posted doesn’t work with taxonomies, it just ignores it.

    wownewmedia

    @wownewmedia

    Hello,

    Me neither, it just ignores the taxonomy and spits out all posts from post_type.

    Anybody got any ideas?

    alanchrishughes

    @alanchrishughes

    @wownewmedia & @alanchrishughes:

    I had the same problem. The WP_query() or query_posts() just spitted out all the posts from my custom post type Factory, ignoring my custom taxonomy selection.

    It started to work after I changed "rewrite" => true in register_taxonomy() to:

    register_taxonomy("factoryType", array("factory"), array("hierarchical" => true, "label" => "Factory Types", "singular_label" => "Factory Type", "rewrite" => array("slug" => "factorytype")));

    in my template file:

    <?php
    $args=array(
      'factorytype' => 'small-factory',
      'post_type' => 'factory',
      'posts_per_page' => 10,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    @hawken1,

    I have my taxonomy registered with the rewrite rule as you mentioned:
    "rewrite" => array("slug" => "taxonomy_name")
    but my query still doesn’t work. It returns 0 posts, even though I know there are some.

    Any one had problems with that and know to to solve it?

    Thanks

    Hm… I think I’ve got mine working.

    Sorry if it’s just stating the obvious, it wasn’t for me 🙁 Spent about 1 hour trying to figure out what I was doing wrong.

    You should use a slug of taxonomy’s term (think slug of a category) as the value for registered taxonomy. I’ll reuse @hawken1 code for my example.

    1) So you have your post type registered as “factory”.

    2) Your taxonomy registered as “factorytype”:

    $args = array(
       "hierarchical" => true,
       "label" => "Factory Types",
       "singular_label" => "Factory Type",
       //this is 'your_registered_taxonomy_name', here 'factorytype'
       "rewrite" => array("slug" => "factorytype")
    );
    
    register_taxonomy("factoryType", array("factory"), $args);

    3) You added some taxonomies terms for your “factory” type (similar as you add categories to a post). So use the slug of your created taxonomy term, not name, not id, but SLUG.
    Say I created a taxonomy term (think category) with the name “Small factory” and slug “small-factory” for the “factory” post type.

    So to get only factories of “Small factory” type, you’d use code as in @hawken1 example:

    <?php
    $args=array(
       //'your_registered_taxonomy_name' => 'taxonomy_term_slug'
      'factorytype' => 'small-factory',
      'post_type' => 'factory',
      'posts_per_page' => 10,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    I got it working slightly differently, using the term and taxonomy parameters. Term contains the slug of the term you’re looking for, taxonomy the id of the taxonomy. So, the earlier example would look like this:

    $args=array(
      'taxonomy' => 'factorytype'
      'term' => 'small-factory',
      'post_type' => 'factory',
      'posts_per_page' => 10,
      'caller_get_posts'=> 1
    );

    @pnd.simpson

    Oh that’s cool – much crearer. Thanks for posting it 🙂

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Custom taxonomy and query_posts’ is closed to new replies.