2013-10-01 61 views
0

我有一個模板文件顯示我的帖子從某一年,它正在工作,但現在有一些帖子在我的網站dublicates,我不允許刪除它們。所以我的問題是,生成的列表顯示兩個不同的職位,但具有相同的內容。我有一個理想,它應該有可能測試post_title是否已經打印到頁面,但我沒有成功。所以,現在我正在尋求幫助。以下是我的模板文件,謝謝。如何在wordpress中刪除重複帖子,帖子裏有不同的id?

 <?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數組運行,並返回一個沒有重複的數組。似乎這樣做,我不會從數據庫中刪除。下面是代碼

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; 
} 
+0

我會建議從數據庫中刪除重複的條目,而不是使用解決方法。 – Mark

+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 
    } 
} 
+0

感謝您的回答,這將刪除數據庫?我不能這樣做。 –

0

您可以使用自定義的SQL查詢的wp_posts表,找到具有不同的標題和內容的帖子的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更多細化。 希望這會有所幫助。