如何根据它的自定义分类术语从多个自定义帖子类型中获取帖子?

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

我有两种自定义的帖子类型和单独的taxonomies 对于每个。

职位类型-dramas --------- 分类法-drama_taxonomyreality_shows -- 分类法-reality_shows_taxonomy现在我正在显示dramas 在显示在术语名称下的页面上发布类型。

代码:

<?php       
//fetching the terms for the \'drama_taxonomy\' taxonomy
//HERE I WANT TO INCLUDE \'reality_shows_taxonomy\' taxonomy too
$terms = get_terms( \'drama_taxonomy\', array(
    \'orderby\'    => \'count\',
    \'hide_empty\' => 0
) );

// now run a query for each dramas terms
foreach( $terms as $term ) {

    // Define the query
    $args = array(
        \'post_type\' => \'dramas\', //HERE I WANT TO INCLUDE \'reality_shows\' custom post type too
        \'drama_taxonomy\' => $term->slug
    );
    $query = new WP_Query( $args ); 
?>
<div class="white-theme">
    <div class="wrapper space">
        <div class="latest-sec">
            <div class="schedule-header">
                <ul class="nav">
                    <li class="content-title"><?php echo $term->name ?></li>
                </ul>
            </div>
            <div style="margin-top:25px;position:relative;padding:0 2%;">
                <div class="video-list-container" id="video-content">
                    <?php 
                        while ( $query->have_posts() ) : $query->the_post(); 
                        $image_id = get_post_thumbnail_id();
                        $image_url = wp_get_attachment_image_src($image_id,\'drama_realityshow_home_page_thumb\', true);  
                        if ( \'reality_shows\' == get_post_type() ) { 
                            $terms = get_the_terms( $post->ID, \'reality_shows_taxonomy\' );
                        }
                        else {
                            $terms = get_the_terms( $post->ID, \'drama_taxonomy\' );
                        }
                    ?>                                                                              
                    <div class="item">
                        <a href="<?php the_permalink(); ?>" class="standard-height">
                        <img src="<?php echo $image_url[0]; ?>" width="300" height="168" class="hoverZoomLink">
                        </a>
                        <div class="new-cat">
                            <?php 
                                foreach($terms as $term) {
                                  echo $term->name; // HERE I WANT TO GET TERMS FROM BOTH TAXONOMIES
                                } ?>
                        </div>
                    </div>
                    <?php endwhile; ?>
                </div>
                <a class="load-more" href="javascript:load_more()" style="display: none;"></a>
            </div>
        </div>
    </div>
</div>
<?php } ?
我想从reality_shows 也应显示在terms 名称

如何根据上述需要修改/更新此代码?

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

您最好的解决方案是运行single 查询要查询的位置

所有帖子类型

无论分类如何,所有帖子都来自

一旦您拥有了所有这些,就可以使用usort() 对结果进行相应排序。我不知道您希望您的帖子类型如何排序,但假设您希望按帖子类型和期限排序

好的,让我们看一下代码,但在开始之前,让我们看一些重要的注释

需要考虑的重要注意事项以下代码未经测试,因此可能存在错误。建议在启用调试的情况下首先在本地运行此操作

该代码至少需要PHP5。4,并且由于短数组语法和直接数组解引用,在旧版本上会导致致命错误。如果您仍在使用旧版本,建议您升级。PHP5之前的所有版本。4已经下线,如果您仍在使用这些版本,则会给您的站点带来巨大的安全风险

  • VERY VERY IMPORTANT: 中的排序功能usort() 只有在查询中有一个或两个帖子类型时,才会按预期工作;如果有两个以上的帖子类型,则函数中的逻辑将无法正常工作。此外,如果每个帖子类型都分配了一个分类法,那么逻辑也会像预期的那样工作。此外,如果任何职位分配了多个术语,则只有第一个术语用于排序

    我将在编写代码时解释代码,以便于理解

    代码

  • // Define our query arguments
    $args = [
        \'post_type\' => [\'dramas\', \'reality_shows\'], // Get posts from these two post types
        // Define any additional arguments here, but do not add taxonomy parameters
    ];
    $q = new WP_Query( $args );
    
    /**
     * We now need to sort the returned array of posts stored in $q->posts, we will use usort()
     *
     * There is a bug in usort causing the following error:
     * usort(): Array was modified by the user comparison function
     * @see https://bugs.php.net/bug.php?id=50688
     * This bug has yet to be fixed, when, no one knows. The only workaround is to suppress the error reporting
     * by using the @ sign before usort
     *
     * UPDATE FROM COMMENTS FROM @ birgire
     * The usort bug has been fixed in PHP 7, yeah!!
     */
    @usort( $q->posts, function ( $a, $b )
    {
        // Sort by post type if two posts being compared does not share the same post type
        if ( $a->post_type != $b->post_type )
            return strcasecmp( $a->post_type, $b->post_type ); // Swop the two variable around for desc sorting
    
        /**
         * I we reach this point, it means the two posts being compared has the same post type
         * We will now sort by taxonomy terms
    
         * The logic for the code directly below is, we have two post types, and a single post can only be assigned to 
         * one post type, so a post can only be from the dramas post type or from the reality_shows post type. We also just
         * need to check one of the two posts being compared as we know both posts are from the same post type. If they
         * they were not, we would not have been here
         *
         * The socond part of the logic is that the drama_taxonomy is only assigned to the dramas post type and the 
         * reality_shows_taxonomy is only assigned to the reality_shows post type, so we can set the taxonomy according
         * to post type
         */
        $taxonomy = ( $a->post_type == \'dramas\' ) ? \'drama_taxonomy\' : \'reality_shows_taxonomy\';
        $terms_a = get_the_terms( $a->ID, $taxonomy );
        $array_a = ( $terms_a && !is_wp_error( $terms_a ) ) ? $terms_a[0]->name : \'zzzzz\'; // zzzz is some crappy fallback
    
        $terms_b = get_the_terms( $b->ID, $taxonomy );
        $array_b = ( $terms_b && !is_wp_error( $terms_b ) ) ? $terms_b[0]->name : \'zzzzz\'; // zzzz is some crappy fallback
    
        // We will now sort by terms, if the terms are the same between two posts being compared, sort by date
        if ( $array_a != $array_b ) {
            return strcasecmp( $array_a, $array_b ); // Swop the two variables around to swop sorting order
        } else {
            return $a->post_date < $b->post_date; // Change < to > to swop sorting order around
        }
    }, 10, 2 );
    
    // $q->posts is now reordered and sorted by post type and by term, simply run our loop now
    
    你的循环应该是这样的。您需要对此进行修改以满足您的确切需要

    if ( $q->have_posts() ) {
        // Define variable to hold previous post term name
        $term_string = \'\';
        while ( $q->have_posts() ) {
        $q->the_post();
    
           // Set the taxonomy according to post type
          $taxonomy = ( \'reality_shows\' == get_post_type() ) ? \'reality_shows_taxonomy\' : \'drama_taxonomy\';
            // Get the post terms. Use the first term\'s name
            $terms = get_the_terms( get_the_ID, $taxonomy );
          $term_name = ( $terms && !is_wp_error( $terms ) ) ? $terms[0]->name : \'Not assigned\';
            // Display the taxonomy name if previous and current post term name don\'t match
            if ( $term_string != $term_name )
                echo \'<h2>\' . $term_name . \'</h2>\'; // Add styling and tags to suite your needs
    
            // Update the $term_string variable
            $term_string = $term_name;
    
            // REST OF YOUR LOOP
    
        }
     wp_reset_postdata();
    }