通过多个分类和术语获取所有定制帖子

时间:2014-07-22 作者:user3571316

对于“成员”页面,我有4个自定义分类法,其中包含多个术语。我有一个“位置”和“组织”的分类法,我正试图将其分为一个列表。我需要按照“组织”分类法中的“aba治疗师”一词来获取所有帖子,并按照它们的“位置”来分隔这些帖子如果这对任何人都有意义的话。类似这样:

ABA治疗师组织分类学术语

位置分类学第2学期

位置分类学第3学期

这就是我所拥有的代码,它只会吸引“成员”CPT中的每个帖子,并按每个学期组织:

    foreach ($taxonomies as $taxonomy){
        $terms = get_terms($taxonomy, array(\'orderby\' => \'date\', \'order\' => \'ASC\'));

        if ( !empty( $terms ) && !is_wp_error( $terms ) ){
            foreach ( $terms as $term ) {
                $args = array(
                    \'post_type\'           => $post_type,
                    \'orderby\'             => \'date\',
                    \'order\'               => \'ASC\',
                    \'ignore_sticky_posts\' => 1,
                    \'post_status\'         => \'publish\',
                    \'posts_per_page\'      => - 1,
                    \'tax_query\'           => array(
                        array(
                            \'taxonomy\'    => $taxonomy,
                            \'field\'       => \'slug\',
                            \'terms\'       => $term->slug
                        )
                    )
                );
                $my_query = null;
                $my_query = new WP_Query($args);
                if ($my_query->have_posts()) {
                    echo \'<div class="members"><div class="wrap"><h3>\' . $term->name . \'</h3></div><div class="details"><div class="wrap">\';

                        while ($my_query->have_posts()) : $my_query->the_post(); ?>

                            <a href="<?php the_permalink(); ?>">
                                <?php if ( has_post_thumbnail() ) : ?>
                                    <?php the_post_thumbnail(\'cac-small\'); ?>
                                <?php else : ?>
                                    <img src="<?php echo get_template_directory_uri(); ?>/img/profile-placeholder.png" alt="Default Image">
                                <?php endif; ?>
                                <div class="caption">
                                    <p><?php the_title(); ?></p>
                                </div>
                            </a>

                        <?php
                        endwhile; 
                } // END if have_posts loop
                    echo \'</div></div></div>\'; // Close \'details\', \'wrap\', & \'members\' DIVs
                wp_reset_query(); 
            } // END foreach $terms
        }
    }
?>
我不知道从哪里开始。有人有线索吗?

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

$terms = get_terms(\'location\', array(\'orderby\' => \'date\', \'order\' => \'ASC\'));

foreach( $terms as $term ) {
    $args = array(
        \'post_type\'           => $post_type,
        \'orderby\'             => \'date\',
        \'order\'               => \'ASC\',
        \'ignore_sticky_posts\' => 1,
        \'post_status\'         => \'publish\',
        \'posts_per_page\'      => - 1,
        \'tax_query\'           => array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\'    => \'organize\',
                \'field\'       => \'slug\',
                \'terms\'       => array( \'aba-therapist\' )
            ),
            array(
                \'taxonomy\'    => \'location\',
                \'field\'       => \'slug\',
                \'terms\'       => array( $term->slug )
            )
        )
    );

    $my_query = new WP_Query($args);
    if ($my_query->have_posts()) {
        echo \'<div class="members"><div class="wrap"><h3>\' . $term->name . \'</h3></div><div class="details"><div class="wrap">\';

            while ($my_query->have_posts()) : $my_query->the_post(); ?>

                <a href="<?php the_permalink(); ?>">
                    <?php if ( has_post_thumbnail() ) : ?>
                        <?php the_post_thumbnail(\'cac-small\'); ?>
                    <?php else : ?>
                        <img src="<?php echo get_template_directory_uri(); ?>/img/profile-placeholder.png" alt="Default Image">
                    <?php endif; ?>
                    <div class="caption">
                        <p><?php the_title(); ?></p>
                    </div>
                </a>

            <?php
            endwhile; 
    } // END if have_posts loop
    wp_reset_postdata();
}
希望这段代码为你填充工作。在上方添加ABA治疗师标题。

结束

相关推荐

Set terms in a custom post

我有一个自定义贴子“艺术家”和一个分类/类别“艺术家类别”,我想创建(通过开发)一个新贴子并设置艺术家的类别,但它不起作用。我用以下代码对其进行了测试:function createNewPost( $response ){ global $userMeta; $userID = $response->ID; $user = new WP_User( $userID ); $role = $userMeta->