2010-07-06 56 views
0

我想寫一個自定義SQL查詢,將創建一個最近的帖子和評論列表,我很難想象我如何能做到這一點。WordPress的最新帖子和評論飼料?

我可以通過日期拉最新評論DESC和我可以拉日期DESC最新的帖子,但我如何使飼料/查詢顯示他們兩個?

這裏是我的評論SQL

SELECT comment_id, comment_content, comment_date 
FROM wp_comments 
WHERE comment_author = 'admin' 
ORDER BY comment_date DESC 

編輯:更清晰:

對不起,我應該有一個更加清楚一點。我想有基於這樣的名單,他們發生了什麼日期:

Wordpress post 
wordpress post 
wordpress comment 
wordpress post 
wordpress comment 

因此,如果有人在一個4個月大的發表評論,它仍然會在這個「喂」

回答

0

我認爲最好的方法(和避免使用自定義的SQL)將搶在每個最新的X帖子和X的意見,環路和創建數組合並兩成一個「最近」數據組;

$comments = get_comments('number=X'); 
$posts = get_posts('posts_per_page=X'); 

$most_recent = array(); 

foreach ($comments as $comment) 
    $most_recent[strtotime($comment->comment_date_gmt)] = $comment; 

foreach ($posts as $post) 
    $most_recent[strtotime($post->post_date_gmt)] = $post; 

unset($comments, $posts); 

krsort($most_recent); 
$most_recent = array_slice($most_recent, 0, X); 

foreach ($most_recent as $post_or_comment) { 

    $is_post = isset($post_or_comment->post_date_gmt); 

    // output comments and posts 
} 

這不是太漂亮,但它應該這樣做。

+0

這似乎適用於我。任何方式我都可以獲得評論發佈的帖子的標題? – wesbos 2010-07-06 22:16:53

+0

'$ comment_post_title = get_the_title($ comment-> comment_post_ID);' – TheDeadMedic 2010-07-06 22:42:48

+0

謝謝,現在就試試:) – wesbos 2010-07-06 22:48:57

0

的頂部出現不需要特殊的mySql,只需使用循環中的query_posts循環和get_comments函數。

<?php query_posts('posts_per_page=10'); while (have_posts()) : the_post(); ?> 

    <div> 
     <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"<?php the_title_attribute(); ?></a> 
     <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php echo the_title(); ?></a> 
     <?php 
      $comments = get_comments(); 
      foreach($comments as $comm) : 
       echo($comm->comment_author); 
       echo($comm->comment_content); 
      endforeach; 
     ?> 
    </div> 

<?php endwhile; ?> 
+0

對不起,我應該更清楚一點。請參閱上文。 – wesbos 2010-07-06 20:49:48

1

要獲得完全基於兩個表最近的時間戳列表,你需要使用UNION:

SELECT wc.comment_date AS dt 
    FROM WP_COMMENTS wc 
UNION ALL 
SELECT wp.post_date AS dt 
    FROM WP_POSTS wp 
ORDER BY dt 

...其中dt是用於保存日期值的列列別名用於任一表中的記錄。

使用UNION ALL - 由於數據來自兩個不同的表,因此不會更改要過濾的重複項。但這意味着你必須從任意一個表中根據數據和數據類型得到你想要的其他列...