首页 文章

如何在wordpress中获取所选类别的热门帖子?

提问于
浏览
0

我试图使用coment计数获得热门帖子 . 我还想从查询中排除一些类别 . 知道如何实现这一目标 .

排除特定类别的查询是什么?例如,我希望查询不应包括类别名称 Health 和自动

SELECT ID,post_title,COUNT($ wpdb-> comments.comment_post_ID)AS'stammy'FROM $ wpdb-> posts,$ wpdb-> comments WHERE comment_approved ='1'AND $ wpdb-> posts.ID = $ wpdb- > comments.comment_post_ID AND post_status ='publish'AND post_date <'$ now'AND post_date

2 回答

  • 0

    WordPress中有3个可用的功能可以用来做这个.. query_postsget_postsWP_Query 返回按注释计数排序的帖子选择,不需要SQL查询..

    <?php
    $my_query = new WP_Query;
    $my_query->query( array( 
        'cat' => '1,2,3,-4,-5,-6',
        'orderby' => 'comment_count',
        'order' => 'desc'
    ) );
    if( $my_query->have_posts() ) :
        while( $my_query->have_posts() ) : $my_query->the_post();
    
            ?>
            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <?php the_title(); ?>
    <?php the_content(); ?> </div> <?php endwhile; endif; wp_reset_query(); ?>

    1,2和3是包括的类别,4,5和6是排除,负值表示排除,正常非负数是包含 .

    有关查询的其他可能参数,请参见此处 .
    http://codex.wordpress.org/Function_Reference/query_posts

    此处还有关于post循环内使用的标签的信息( the_titlethe_content 等) . http://codex.wordpress.org/Template_Tags#Post_tags

    希望有帮助...... :)

  • 2

    我刚才这样做了 . 在我的博客上你有解决方案,我知道它的波兰语但代码是代码:) http://blog.grabek-adam.pl/2010/06/wordpress-popularne-posty-plugin-do-obrazkw/

    在这里你只是查询:

    $posty = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS wp_posts.id, wp_posts.post_title, wp_posts.comment_count, wp_posts.post_date
                            FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
                            INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
                            WHERE 1=1  AND wp_term_taxonomy.taxonomy = 'category'
                            AND wp_term_taxonomy.term_id IN ('cat_id1,cat_id2,cat_id5')
                            AND wp_posts.post_type = 'post'
                            AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
                            AND wp_posts.comment_count > 0
                            GROUP BY wp_posts.ID ORDER BY wp_posts.comment_count DESC
                            LIMIT 5
                            ");
    

    此代码仅包含您在此处指定的类别 AND wp_term_taxonomy.term_id IN ("cat_id1,cat_id2,cat_id5") 但我认为根据您的要求更改将很容易

相关问题