如何让这段调用WP热评文章的SQL语句不显示自动草稿

添加到function.php中获取30天热评文章代码

// 获得热评文章   
function simple_get_most_viewed($posts_num=10, $days=30){   
    global $wpdb;   
    $sql = "SELECT `ID` , `post_title` , `comment_count` FROM $wpdb->posts  
            WHERE `post_type` = 'post' AND TO_DAYS( now( ) ) - TO_DAYS( `post_date` ) get_results($sql);   
    $output = "";   
    foreach ($posts as $post){   
        $output .= "\n<li>ID)."\" rel=\"bookmark\" title=\"".$post->post_title."\" >".$post->post_title."</a> - ".$post->comment_count."个评论</li>";   
    }   
    echo $output;   
}   
// 函数 get_permalink($post->ID) 可以得到文章的URL

不过以上代码会调用显示自动草稿,如何加个判断不显示

这是作者原文链接
http://www.wheatime.com/2009/09/wp-hot-sql.html

(上次修改于2011-06-23,查看历史版本。)
共有3个回答 按发表时间排序
  • 2
    最佳答案
    wopus - 2011-06-23

    $sql 部分的修改为这个

    $sql = "SELECT `ID` , `post_title` , `comment_count` FROM $wpdb->posts WHERE `post_type` = 'post' AND `post_status` = 'publish' AND TO_DAYS( now( ) ) - TO_DAYS( `post_date` ) < $days ORDER BY `comment_count` DESC LIMIT 0 , $posts_num ";
    

    增加了判断

    post_status = 'publish'

    知更鸟 - 2011-06-23

    嗯,可以了,胖神果然神,呵呵

  • 1
    ouhinit - 2011-06-23

    SELECT *
    FROM wp_posts
    WHERE post_type = 'post'
    AND post_status = 'publish'
    AND TO_DAYS( now( ) ) - TO_DAYS( post_date ) < 30
    ORDER BY comment_count DESC
    LIMIT 0 , 10

做好事要留名,请先登录再回答,如果还没有帐户请先注册