2012-05-29 68 views
1

我正在創建一個wordpress小部件以顯示過去4天內評論最多的文章。Wordpress查詢在X天內顯示大多數評論文章

我有這個迄今爲止

global $wpdb; 
global $pagepost; 


function filter_where($where = '') { 
    // posts in the last 4 days 
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-4 days')) . "'"; 
    return $where; 
} 

add_filter('posts_where', 'filter_where'); 
$the_query = new WP_Query('posts_per_page=4;orderby=comment_count'); 

remove_filter('posts_where', 'filter_where'); 
?> 
    <ul> 
    <?php while ($the_query->have_posts()) : $the_query->the_post(); 
    echo "<li>"; 
    echo "<a href='".get_permalink()."' class='linkTitle'>"; 
    echo get_the_title(); 
    echo "</a>"; 
    woo_post_meta(); 
    echo "<div class='fix'>"; 
    echo "</li>"; 
endwhile; 
    echo "</ul>"; 
wp_reset_postdata(); 

從什麼我能找到在WordPress網站應該從過去的4天COMMENT_COUNT

責令退還文章,但它只是顯示了我最後的4文章,我敢肯定,我在這裏做的很明顯是錯誤的,但我無法得到它

我想4篇文章發表最多評論文章發佈在過去4天。

有人請保存什麼小頭髮,我已經離開

+1

只是一個查詢問題 - 在這裏使用「;」是否正確? posts_per_page = 4; orderby = comment_count我猜它是&?? – swapnesh

+0

我將它改爲&但沒有改變......它似乎接受了兩個? –

回答

3

感謝@swapnesh我找到了答案。

我的查詢是不正確的,它應該是這樣的

$the_query = new WP_Query(array('posts_per_page' => 4, 'orderby' => 'comment_count', 'order'=> 'DESC')); 
+0

這聽起來很酷,更放心,你找到一個解決方案,你自己用一點提示,upvoted;) – swapnesh

+0

這將返回所有時間的評論最多的帖子,限制評論X天不能用wp_query – janw

+0

@ Janw它可以通過添加我在原始問題中指定的過濾器。它現在在我的網站上工作(www.lazygamer.net),你可以看到趨勢的帖子都是從最近4天開始的 –

0

$ querystr =「 選擇$ wpdb->的帖子。* FROM $ wpdb->帖子WHERE 1 = 1 AND WPDB $ - > posts.post_status ='publish'AND $ wpdb-> posts.post_type ='post'ORDER BY $ wpdb-> posts.comment_count DESC 「;

使用此自定義查詢

$ pageposts = $ wpdb-> get_results($ querystr,OBJECT); echo'

'; print_r($pageposts); echo'
';

相關問題