【问题标题】:Exclude duplicated posts from wordpress custom queries从 wordpress 自定义查询中排除重复的帖子
【发布时间】:2013-11-27 15:14:45
【问题描述】:

我正在为主页构建一个模板,该模板在顶部显示 4 个最新帖子,并且在页面周围按类别划分了一些帖子组(我正在使用 get_posts 执行查询)。

我想做的是从这些类别的帖子中排除任何已经出现在最新四则新闻中的帖子。

我想我应该得到这四个帖子 ID 并在“类别”查询中使用“post__not_in”参数,但我无法让它工作。

你有什么提示吗?

这是代码:

// First query: I get last four posts

$args = array(

    'post_type'        => 'post',
    'post_status'      => 'publish',
    'numberposts'           => 4

);

// In my dreams, this should be the array of post IDs

$ids->query_vars['page_id'];

// Second query: I get 5 posts for a given categoory and exclude posts with IDs from first query

$query = new WP_Query($args);

$args2 = array(

    'numberposts'   => 5,
    'category'         => 15,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post__not_in' => $ids

);
$query2 = new WP_Query($args2);

$primaposizione = get_posts( $args2 );
foreach ( $primaposizione as $post ) : setup_postdata( $post );

... do the stuff ...

endforeach;
wp_reset_postdata();

更新

<?php
    $args = array(
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'posts_per_page'           => 4
    );

    $query = new WP_Query($args);
    $excludeID = array();
    while ( $query->have_posts() ) : $query->the_post();
    $excludeID = $post->ID;
    endwhile;

        $args = array(
            'posts_per_page'   => 1,
            'orderby'          => 'post_date',
            'order'            => 'DESC',
                    'category'         => 15,
            'post_type'        => 'post',
            'post_status'      => 'publish',
            'post__not_in' => array($excludeID)
        );

        $primaposizione = get_posts( $args );
        foreach ( $primaposizione as $post ) : setup_postdata( $post );
        $category = get_the_category();
        $slug = $category[0]->category_nicename ;
        $esteso = $category[0]->cat_name;
        if(has_post_thumbnail()) { ?>
            <span class="hidden-xs"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('bones-thumb-300', array('class' => 'img-responsive')) ?></a></span>
            <?php } ?>
            <h3 class="ellipsis"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt();?>
        <?php endforeach;
    wp_reset_postdata();
?>

【问题讨论】:

  • 你在正确的轨道上。请发布一些代码进行评论。
  • 你说得对,我刚刚粘贴了我的代码。

标签: wordpress posts


【解决方案1】:

您尝试选择 ID 的方式行不通。您必须先调用 wp_query。我认为这应该可行:

$args = array(
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'numberposts'           => 4
);

$query = new WP_Query($args);
$excludeID = array();

while ( $query->have_posts() ) : $query->the_post(); ?>
     $excludeID[] = $post->ID;       // forgot the brackets
     // do stuff
endwhile;

(...)

【讨论】:

  • 感谢 Koen,我想我已经接近了,但我仍然缺少一些东西:我只得到第四个帖子的 ID 而不是数组。我可能在语法上做错了什么。你介意看看我粘贴的更新代码吗?
  • 对不起,我忘记了 $excludeID 后面的括号。将其更改为 $exclude[] = $post->ID;并在此处删除 Array() 'post__not_in' => $excludeID
  • 我很乐意这样做,但我需要至少 15 个声望点 :(
猜你喜欢
  • 2014-03-05
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多