• 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 7 replies - 16 through 22 (of 22 total)
  • Thanks so much guys, works like a charm.

    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.

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

    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 @ https://www.ads-software.com/extend/plugins/postpage-content-anchor-tabset/.

    Thanks.
    Shazzad

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

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

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

    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(); ?>

    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.

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