避免来自相同分类的重复帖子

时间:2017-11-06 作者:cbtr

我用这个来检索一个名为“works“基于名为”works_year“。

如果我选择了该分类法(works\\u year)的1个以上值,则该帖子将被复制到我的首页。

我可以避免吗?

$years_loop = get_terms(
    array(
        \'taxonomy\'  => \'works_year\',
        \'orderby\'   => \'slug\',
        \'order\'     => \'DESC\',
    )
);
foreach($years_loop as $year_loop_slug) :

$args = array(
    \'post_type\'       => \'works\',
    \'posts_per_page\'  => -1,
    \'tax_query\'       => array(
        array(
            \'taxonomy\'  => \'works_year\',
            \'field\'     => \'slug\',
            \'terms\'     => array( $year_loop_slug->slug ),
            \'operator\'  => \'IN\'
        )
    ),
);

$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();

//Show the Post

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

我认为有两种方法可以做到这一点,两者都非常相似。

A) 您可以收集数组中显示的帖子的ID,然后排除具有post__not_in 类似这样的查询参数:

$years_loop = get_terms(
    array(
        \'taxonomy\'  => \'works_year\',
        \'orderby\'   => \'slug\',
        \'order\'     => \'DESC\',
        )
);

// array for displayed posts, we will update it with every loop
$displayed = [];

foreach($years_loop as $year_loop_slug) :
    $args = array(
        \'post_type\'       => \'works\',
        \'posts_per_page\'  => -1,
        \'post__not_in\'    => array_unique( $displayed ),
        \'tax_query\'       => array(
            array(
                \'taxonomy\'  => \'works_year\',
                \'field\'     => \'slug\',
                \'terms\'     => array( $year_loop_slug->slug ),
                \'operator\'  => \'IN\',
            )
        ),
    );
    $loop = new WP_Query($args);
    while ($loop->have_posts()) : $loop->the_post();

        // update array with currently displayed post ID
        $displayed[] = get_the_ID();

        //your output

    endwhile;
endforeach;
B)如果您想避免使用post__not_in 参数,这可能会对站点性能产生不良影响,您可以检查循环中的重复项:

$years_loop = get_terms(
    array(
        \'taxonomy\'  => \'works_year\',
        \'orderby\'   => \'slug\',
        \'order\'     => \'DESC\',
        )
);

// array for displayed posts, we will update it with every loop
$displayed = [];

foreach($years_loop as $year_loop_slug) :
    $args = array(
        \'post_type\'       => \'works\',
        \'posts_per_page\'  => -1,
        \'tax_query\'       => array(
            array(
                \'taxonomy\'  => \'works_year\',
                \'field\'     => \'slug\',
                \'terms\'     => array( $year_loop_slug->slug ),
                \'operator\'  => \'IN\',
            )
        ),
    );

    $loop = new WP_Query($args);
    while ($loop->have_posts()) : $loop->the_post();
        // if the current post was already displayed, move on to the next iteration of this loop
        if ( in_array( get_the_ID(), $displayed ) ){
            continue;
        }
        // update array with currently displayed post ID
        $displayed[] = get_the_ID();

        //your output

    endwhile;
endforeach;
代码未经测试,但您已经明白了。

结束

相关推荐

Taxonomy Pagination Rewrite

我有一个名为“推荐信”的自定义帖子类型,其中包含reviews. 我还有一个名为“推荐部门”的分类法reviews. 目标是我将拥有像/reviews/sales/ 或/reviews/service/. 除了分页之外,一切都按照我的意愿进行。我尝试了所有不同的提示和技巧,以使分页按预期工作,但不管怎样/reviews/sales/page/2 就是不管用。但是,如果我手动输入/reviews/sales/?page=2 正如人们所期望的那样,这将导致第二页的结果。是否有允许我使用查询字符串的重写规则?p