0

我有一个模板文件,其中显示了我某年的帖子,它正在工作,但现在我的网站上有一些帖子的重复,我不允许删除它们。所以我的问题是生成的列表显示了两个不同的帖子,但内容相同。我有一个想法,应该可以测试 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 数组运行并返回一个没有重复的数组的函数。似乎做到了,我没有从数据库中删除。这是代码

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;
}
4

2 回答 2

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 中使用自定义参数进行更精细化。希望这会有所帮助。

于 2013-10-01T12:44:24.483 回答
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
   }
}
于 2013-10-01T11:23:56.550 回答