是否仅检索具有两个必需术语的自定义帖子类型(分别来自不同的自定义分类)?

时间:2011-02-07 作者:janoChen

我创建了一个自定义帖子类型和两个自定义分类法。

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\' ),
       ));
我使用以下方法检索它们:

<?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; ?>
问题是,任何具有自定义分类法位置=首页的帖子都会出现在第一个自定义循环中。

第一个自定义循环应该只检索具有这两个术语(来自不同分类法)的自定义帖子:位置=首页,部分=概要。

(如果只缺少一个术语,则不应检索帖子)

有什么建议吗?

2 个回复
SO网友:janoChen

下载Wordpress 3.1 RC3解决了这个问题。

SO网友:Simon Blackbourn

是的,wordpress 3.1通过“高级分类查询”正好满足了这一要求。

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

结束

相关推荐