2013-02-16 85 views
0

我已經嘗試了以下鏈接上的解決方案,但是,我可以不讓他們顯示帖子!WordPress的,排序帖子按評論計數

http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query 
http://wordpress.stackexchange.com/questions/1529/displaying-posts-depending-on-number-of-comments-and-latest-comments 
http://wordpress.org/support/topic/sort-by-number-of-comments 

這是我的默認代碼,我的問題是,我如何獲得此代碼按評論計數排序?

<?php 
/* 
Template Name: Homepage 
*/ 
get_header(); global $gp_settings, $user_ID, $user_identity, $user_level; 

$referrer = $_SERVER['HTTP_REFERER']; 

?> 

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 


    <!-- BEGIN CONTENT --> 

    <div id="content"> 


     <!-- BEGIN POST CONTENT --> 

     <?php if($post->post_content) { ?> 

      <div id="post-content"> 
       <?php the_content(__('Read More &raquo;', 'gp_lang')); ?> 
      </div> 

      <?php } ?> 

     <!-- END POST CONTENT -->   


    </div> 

    <!-- END CONTENT -->  


<?php endwhile; endif; ?> 


<?php get_footer(); ?> 

這是我曾嘗試沒有成功

<?php 
/* 
Template Name: Homepage 
*/ 
get_header(); global $gp_settings, $user_ID, $user_identity, $user_level; 

$referrer = $_SERVER['HTTP_REFERER']; 

?> 
<?php 

$querystr = " 
SELECT $wpdb->posts.* 
FROM $wpdb->posts, $wpdb->postmeta 
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
AND $wpdb->postmeta.meta_key = 'tag' 
AND $wpdb->postmeta.meta_value = 'email' 
AND $wpdb->posts.post_status = 'publish' 
AND $wpdb->posts.post_type = 'post' 
AND $wpdb->posts.post_date < NOW() 
ORDER BY $wpdb->posts.post_date DESC 
"; 

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

?> 
<?php if ($pageposts): ?> 
<?php global $post; ?> 
<?php foreach ($pageposts as $post): ?> 
<?php setup_postdata($post); ?> 

    <!-- BEGIN CONTENT --> 

    <div id="content"> 


    <!-- BEGIN POST CONTENT --> 

    <?php if($post->post_content) { ?> 

     <div id="post-content"> 
      <?php the_content(__('Read More &raquo;', 'gp_lang')); ?> 
     </div> 

    <?php } ?> 

    <!-- END POST CONTENT -->   


</div> 

<!-- END CONTENT -->  

<?php endforeach; ?> 
<?php endif; ?> 

<?php get_footer(); ?> 

回答

0

經驗法則的代碼的例子:如果你正在運行使用默認的WordPress表的SQL查詢,你可能就錯。

假設在你的頁面的原始查詢即將到來的職位是正確的,你只是要修改它們的順序,可以使用主循環的原始參數來構建通過,而不是一個新的查詢和循環:

<?php 
/* 
Template Name: Homepage 
*/ 
get_header(); global $gp_settings, $user_ID, $user_identity, $user_level; 
$referrer = $_SERVER['HTTP_REFERER']; 

global $wp_query; //Gets the default query 
$default_args = $wp_query->query; //Grabs the original arguments 
/* 
If you find that the above line returns a string rather than an array, 
use this instead: 
*/ 
//parse_str($wp_query->query, $default_args); 
$default_args['orderby'] = 'comment_count'; //Set the new order parameter 
$q = new WP_Query($default_args); //Generate a new Query 

if ($q->have_posts()) : while ($q->have_posts()) : $q->the_post(); //Loop through new Query ?> 
    <!-- BEGIN CONTENT --> 
    <div id="content"> 
     <!-- BEGIN POST CONTENT --> 
     <?php if($post->post_content) { ?> 
      <div id="post-content"> 
       <?php the_content(__('Read More &raquo;', 'gp_lang')); ?> 
      </div> 
      <?php } ?> 
     <!-- END POST CONTENT -->   
    </div> 
    <!-- END CONTENT --> 
<?php 
endwhile; endif; 
get_footer(); 
?> 
+0

請記住,'comment_count'參數僅適用於Wordpress 2.9或更高版本。 – maiorano84 2013-02-16 05:00:37

+1

我使用的是一個名爲buddy的好友新聞主題,顯示附加到帖子的圖像縮略圖,您的示例只返回評論,我將如何去查找好友主題正在使用的修改後的查詢,以便我可以使用該查詢並添加comment_count按順序排列? – 2013-02-17 01:23:20