是否通过查询分类和分类中的某些术语来获取帖子?

时间:2015-09-09 作者:Foolish Coder

我有一个分类法叫做schedule_day_taxonomy

add_action( \'init\', \'schedule_day_tax_func\' );
function schedule_day_tax_func() {
    register_taxonomy(
        \'schedule_day_taxonomy\',
        \'schedule\',
        array(
            \'hierarchical\' => true,
            \'label\' => \'Day\',
            \'query_var\' => true,
            \'orderby\'  => \'ID\', /*
        )
    );
}
还有这个taxonomy 仅分配给某个自定义职位类型。。调用schedule

add_action( \'init\', \'schedule_func\' );

function schedule_func(){
    register_post_type(\'schedule\', array(
        \'label\' => \'TV schedule\',
        \'show_ui\' => true,
        \'supports\' => array(\'title\', \'thumbnail\'),
        \'public\' => true,
        \'publicly_queryable\' => true, 
        \'rewrite\' => array( \'slug\' => \'show-schedule\' ),
        \'labels\' => array (
            \'name\' => \'TV schedule\',
            \'singular_name\' => \'TV schedule\',
            \'menu_name\' => \'TV Schedules\'
        ),
    ) );
}
现在我想把邮件发过去terms 在分类法下创建schedule_day_taxonomy 仅为自定义职位类型分配schedule

我创建了两个术语mondaytuesday

如果是在规定期限内,我正在努力获得所有的职位tuesday. 所以我尝试了以下代码:

<ul id="home-sche-list">
    <?php
        $args = array( \'post_type\' => \'schedule\', \'posts_per_page\' => 15, \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'schedule_day_taxonomy\',
                    \'field\' => \'tuesday\',
                    \'terms\' => \'tuesday\'
                )
            )                    
                                                );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();                           
        $image_id = get_post_thumbnail_id();
        $image_url = wp_get_attachment_image_src($image_id,\'schedule_home_page_thumb\', true);   
        $terms = get_the_terms( $post->ID, \'schedule_taxonomy\' ); 
    ?>
    <li>
        <a href="<?php the_permalink(); ?>" target="_blank" class="wrapper-container">
            <div class="sche-thumb-con">
                <span>
                <img src="<?php echo $image_url[0]; ?>" class="video-thumb hoverZoomLink" alt=""/>                                      
                    <div class="new-cat">
                    <?php 
                        foreach($terms as $term) {
                          echo $term->name;
                        } ?>
                    </div>                                              
                </span>
            </div>
            <div class="sche-txt-wrapper">
                <div class="schedule-prog"><?php the_title(); ?></div>                              
                <div class="schedule-time evening">
                <?php 
                    $terms = get_the_terms( $post->ID, \'schedule_time_taxonomy\' ); 
                    foreach($terms as $term) {
                          echo $term->name;
                        }                                           
                ?>
                </div>
            </div>
        </a>
    </li>
    <?php endwhile; ?>                              
</ul>
但我没有收到显示的帖子。我有一个职位分配给这个学期tuesday.

DAY

Cat

Admin address bar

address

我做错了什么?如果我把这篇文章分配给另外两个分类法(自定义),会有影响吗?我还有两个分类法,它们被分配给了这篇文章。

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

这个field 中的参数tax_query 应设置为slug 看来你已经通过了鼻涕虫这个术语。传递给的任何值field 除了slugname 将导致field 参数默认为term_id.

始终记住,传递给的值field 应与传递给的内容匹配terms, 所以,如果terms 是术语slugs,field 应该是slug

编辑评论中OP的最终解决方案

$args = array( 
    \'post_type\' => \'schedule\', 
    \'posts_per_page\' => 15, 
    \'tax_query\' => array( 
        array( 
            \'taxonomy\' => \'schedule_day_taxonomy\', 
            \'field\' => \'term_id\', 
            \'terms\' => 1278 
        ) 
    )
);