0

I created one custom post type and two custom taxonomies.

register_post_type('blocks',array(
       'labels' => array(
         'name' => __( 'Blocks' ),
         'singular_name' => __( 'Block' ),
         'add_new_item' => 'Add New Block',
         'edit_item' => 'Edit Block',
         'new_item' => 'New Block',
         'search_items' => 'Search Block',
         'not_found' => 'No Block found',
         'not_found_in_trash' => 'No Blocks found in trash',
       ),
       'public' => true,
       'hierarchical' => false,
       'taxonomies' => array( 'section'),
       'supports' => array('title','editor','thumbnail','custom-fields'),
       'rewrite' => array('slug'=>'blocks','with_front'=>false),
     ));

    register_taxonomy('location','blocks',array(
       'hierarchical' => true,
       'labels' => array(
         'name' => __( 'Location' ),
         'singular_name' => __( 'Location' ),
         'add_new_item' => 'Add New Location',
         'edit_item' => 'Edit Location',
         'new_item' => 'New Location',
         'search_items' => 'Search Location',
         'not_found' => 'No Locations found',
         'not_found_in_trash' => 'No Locations found in trash',
         'all_items' => __( 'All Sections' ),
       ),
       'query_var' => true,
       'rewrite' => array( 'slug' => 'location' ),
       ));
     register_taxonomy('section','blocks',array(
       'hierarchical' => true,
       'labels' => array(
         'name' => __( 'Section' ),
         'singular_name' => __( 'Section' ),
         'add_new_item' => 'Add New Section',
         'edit_item' => 'Edit Section',
         'new_item' => 'New Section',
         'search_items' => 'Search Section',
         'not_found' => 'No Sections found',
         'not_found_in_trash' => 'No Sections found in trash',
         'all_items' => __( 'All Sections' ),
       ),
       'query_var' => true,
       'rewrite' => array( 'slug' => 'section' ),
       ));

I'm using the following to retrieve them:

<?php // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query('post_type=blocks&location=Front Page&section=Profile');
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
    <div class="block-1">
        <?php the_post_thumbnail('large'); ?>
    </div>
<?php endwhile; ?>


    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=blocks&location=Front Page&section=Theme Left');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>
        <div class="float-left">
            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a>
            <p><?php the_excerpt(); ?></p>
        </div>
    <?php endwhile; ?>

The problem is that any posts that has the custom taxonomy Location = Front Page appear in the first custom loop.

It shouldn't. The first custom loop should ONLY retrieve custom posts which has these two terms (from different taxonomies): Location = Front Page and Section = Profile.

(If only one term is missing the posts shouldn't be retrieved)

Any suggestions?

  • Please mark your answer as solution (this does work). – hakre Feb 7 '11 at 23:17
2

Well, downloading Wordpress 3.1 RC3 solved the problem.

1

yes, wordpress 3.1 addresses exactly this requirement with "advanced taxonomy queries".

otto's done a great in-depth explanation at http://ottopress.com/2010/wordpress-3-1-advanced-taxonomy-queries/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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