WordPress.org

Ready to get started?Download WordPress

Forums

Custom taxonomy and query_posts (23 posts)

  1. Leroy12
    Member
    Posted 4 years ago #

    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

  2. MichaelH
    Member
    Posted 4 years ago #

    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.

  3. Leroy12
    Member
    Posted 4 years ago #

    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...

  4. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>
  5. phejster
    Member
    Posted 3 years ago #

    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().
    ?>
  6. JoeyBottle
    Member
    Posted 3 years ago #

    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

  7. JoeyBottle
    Member
    Posted 3 years ago #

    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

  8. alanchrishughes
    Member
    Posted 3 years ago #

    @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.

  9. alanchrishughes
    Member
    Posted 3 years ago #

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

  10. wownewmedia
    Member
    Posted 3 years ago #

    Hello,

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

    Anybody got any ideas?

  11. alanchrishughes
    Member
    Posted 3 years ago #

    ...

  12. hawken1
    Member
    Posted 3 years ago #

    @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().
    ?>
  13. dashaluna
    Member
    Posted 3 years ago #

    @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

  14. dashaluna
    Member
    Posted 3 years ago #

    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().
    ?>
  15. pnd.simpson
    Member
    Posted 3 years ago #

    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
    );
  16. dashaluna
    Member
    Posted 3 years ago #

    @pnd.simpson

    Oh that's cool - much crearer. Thanks for posting it :)

  17. alexo05
    Member
    Posted 3 years ago #

    Thanks so much guys, works like a charm.

  18. Shazzad Hossain Khan
    Member
    Posted 3 years ago #

    Hi guys,
    I have survived a lot with taxonomy and terms. At last i found a solution at least. I have tried with everything, like witting custom query arguments, using that taxonomies and term fields and lot more other way. At last i found a solution but it is also like two pre-test before the main test. Here we go with the codes...
    If you want to do the query with a post id, first retrieve the term ids array.
    $term_ids = wp_get_object_terms( $post_id, $taxonomies );

    Now we have our term ids array. Let retrieve the post ids associated with our terms.
    $post_ids = get_objects_in_term( $term_ids, $taxonomies ) ;
    Now we got all of the post ids array. So you should know the next step i think.

    $query_args = array( 'post__in' => $post_ids, 'post_type' => $post_type, 'post_status' => $post_status ) ;
    query_posts( $query_args ) ;
    ............................

    I am not sure if it can help you, but this codes really works. You can do a single term query with the simple term and taxonomy arguments, but this method is for multiple terms query together.

  19. deb_j
    Member
    Posted 3 years ago #

    @Md. Shazzad Hossain:

    You're a lifesaver -- been struggling to find a clear approach to retrieve custom posts based on multiple custom taxonomy terms for quite a while, and this is it.

    One extra note: for this to work, term ids must have the additional argument set to return term IDs only i.e.

    $args=array('fields'=>'ids'); $term_ids = wp_get_object_terms( $post_id, $taxonomy, $args);
    $post_ids = get_objects_in_term( $term_ids, $taxonomy ) ;

    Then it works like a charm.

  20. Shazzad Hossain Khan
    Member
    Posted 3 years ago #

    Hi deb__j,
    Thanks for the correction. I have missed that part. I have wrote a Post/Page Tabset plugin,my first plugin :). Hope you can take a look @ http://wordpress.org/extend/plugins/postpage-content-anchor-tabset/.

    Thanks.
    Shazzad

  21. microkid
    Member
    Posted 3 years ago #

    Note that WP 3.1 will bring a great improvement for quering (multiple) custom taxonomies:

    http://ottopress.com/2010/wordpress-3-1-advanced-taxonomy-queries/

    So don't spend too much time on creating workarounds!

  22. alanchrishughes
    Member
    Posted 3 years ago #

    This still doesn't work. To register the taxonomy I have...

    add_action( 'init', 'build_taxonomies', 0 );
    
    function build_taxonomies() {
    
    register_taxonomy(
    'HEshowdate',
    'HEshow',
    
    array(
    'hierarchical' => true,
    'label' => 'Date of Show',
    'rewrite' => array('slug' => 'heardeverywhere/showdate'),
    'query_var' => true,
    )
    );
    }

    And to display the posts I have this but it still ignore the taxonomy.

    <?php $loop = new WP_Query( array('post_type' => 'HEshow', 'heardeverywhere/showdate' => 'upcoming', 'orderby' => 'title', 'order' => 'asc', 'posts_per_page' => 5 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    <div class="HeardSidebarShows">
    
    <h3 class="HeardSidebarShow"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    
    <?php the_excerpt(); ?>
  23. alanchrishughes
    Member
    Posted 3 years ago #

    Ahhhh! Don't use capital letters in your taxonomy name. After how long now I just discovered this today, they will work in every other way with capital letters, but not to display them, which is the most important part of all.

Topic Closed

This topic has been closed to new replies.

About this Topic