如何删除wordpress中的重复帖子,其中帖子有不同的ID?

时间:2013-10-01 11:07:48

标签: php wordpress

我有一个模板文件显示我某年的帖子,它正在运行,但现在我的网站上有一些帖子重复,我不允许删除它们。所以我的问题是生成的列表显示两个不同的帖子但具有相同的内容。我有一个想法,应该可以测试post_title是否已经打印到页面,但我没有成功。所以现在我正在寻求帮助。下面是我的模板文件。

        <?php
/* Template Name: 2012 */

get_header();

?>


<?php
    $post_titles = array();
    $year = isset($_GET['y']) ? $_GET['y'] : date('Y');
    if($year == date('Y')){
        $posts = get_newest_posts();
    }
    else{
        $posts = get_yearly_posts($year);
    } ?>
    <div class="site-content-wrapper site-content-blog-wrapper container columns clearfix archive">
    <article id="primary-wrapper" class="eight column">
    <div class="post-blog inner">
    <h2 class="entry-title"><a href="#">News <?= $year ?></a></h2>
    <?php foreach($posts as $post): ?>

    /*  here i would like a test to see if a post with a similar title already has 
        printet to page. if it has not the code below should run and print post to page. */

        <div class="archive-post">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tbody><tr>
            <td width="100%"><span class="STRONG"><a href="<?= bloginfo('url') ?>/?p=<?= $post->ID ?>" target="_top"><?= $post->post_title ?></a></span></td>
        </tr>
        <tr>
            <td><span class="norm">
                <?php if($year != date('Y')): ?>
                    <?= $post->search_plus_description ?>
                <?php else: ?>
                    <?= $post->post_excerpt ?>
                <?php endif ?>
            </span></td>
        </tr>
        <tr>
            <td>
                <span class="norm">
                    <?= $year == date('Y') ? date('d F Y', strtotime($post->post_date)) : date('d F Y', strtotime($post->publication_date)) ?>
                </span>
            </td>
        </tr>
        <tr>
            <td><img src="x.gif" width="1" height="10" alt=""></td>
        </tr>
        </tbody></table>
        </div>

    <?php endforeach ?>

感谢您的回答。我创建的函数运行$ post数组并返回一个没有重复的数组。似乎这样做,我不会从db中删除。这是代码

function remove_duplicates(array $array){
  $tmp_array = array();
$title_array = array();
$date_array = array();
  foreach($array as $post)  
  {
    $title = $post->post_title;
    $date = $post->post_date;
     if (!(in_array($title, $title_array) && in_array($date, $date_array)))
     {
       $tmp_array[] = $post;
    $title_array[] = $title;
    $date_array[] = $date;
     }
  }
  return $tmp_array;
}

2 个答案:

答案 0 :(得分:0)

试试这个

global $wpdb;

$duplicate_titles = $wpdb->get_col("SELECT post_title FROM {$wpdb->posts} GROUP BY post_title HAVING COUNT(*) > 1");

foreach( $duplicate_titles as $title ) {
   $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_title=%s", $title ) ); 
   // Iterate over the second ID with this post title till the last
   foreach( array_slice( $post_ids, 1 ) as $post_id ) {
       wp_delete_post( $post_id, true ); // Force delete this post
   }
}

答案 1 :(得分:0)

您可以在wp_posts表上使用自定义sql查询来查找具有不同标题和内容的帖子的ID。之后,您可以使用WP_Query来使用这些ID来显示模板循环中的帖子。

我发布了一个示例,根据标题给出了不同的帖子:

$sql = 'select ID,post_title,post_type,post_status from wp_posts group by post_title having (post_status)="publish" AND (post_status)="publish"';
$results = $wpdb->get_results($sql,ARRAY_A);
    $distinctIds = array();
    foreach($results as $result){
            $distinctIds[] = $result['ID'];
    }
$args = array('post_type' => 'post','post__in'=> $distinctIds);
$distinct_posts = new WP_Query($args);
if($distinct_posts->have_posts()) : 
    while($distinct_posts->have_posts()) : 
    $distinct_posts->the_post();
    // your stuff related to post
    // echo content etc..
   endwhile;
endif;

当然,我们可以在WP_Query中使用自定义参数进行更细化。 希望这会有所帮助。