如何获取 WordPress 中选定类别的热门帖子?

发布于 2024-10-08 05:12:38 字数 399 浏览 0 评论 0原文

我正在尝试使用评论数来获取热门帖子。我还想从查询中排除某些类别。知道如何实现这一点。

查询什么来排除特定类别?例如,我希望查询不应包含类别名称 health 和 auto

选择 ID、帖子标题、 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 = '发布' AND 发布日期 < '$now' 和 post_date

I am trying to get popular posts using the coment count. I also want to exclude some categories from the query. Any idea how this can be achieved.

What would be query to exclude particular categories? for example i want the query should not include category names health and auto

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煞人兵器 2024-10-15 05:12:38

WordPress 中有 3 个可用函数可以用来执行此操作。query_postsget_postsWP_Query 返回按以下顺序排序的帖子选择评论计数,无需SQL查询。1、2、3

<?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(); ?><br />
            <?php the_content(); ?>
        </div>
        <?php

    endwhile;
endif;
wp_reset_query();
?>

是要包含的类别,4、5、6是排除,负值表示排除,正常非负数是包含。

请参阅此处了解查询的其他可能参数。
http://codex.wordpress.org/Function_Reference/query_posts

这里还提供有关内部使用的标签的信息后循环(the_titlethe_content 等)。
http://codex.wordpress.org/Template_Tags#Post_tags

希望有所帮助...:)

There are 3 available functions in WordPress you can use to do this.. query_posts, get_posts or WP_Query to return a selection of posts ordered by the comment count, no need for the SQL query..

<?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(); ?><br />
            <?php the_content(); ?>
        </div>
        <?php

    endwhile;
endif;
wp_reset_query();
?>

1, 2 and 3 are categories to include, 4, 5 and 6 are exclusions, the negative value indicates an exclusion, normal non-negatives are inclusions.

See here for other possible parameters for the query.
http://codex.wordpress.org/Function_Reference/query_posts

Also here for information on tags used inside the post loop(the_title, the_content, etc).
http://codex.wordpress.org/Template_Tags#Post_tags

Hope that helps... :)

铁轨上的流浪者 2024-10-15 05:12:38

我不久前就做过这个。
在我的博客上你有解决方案,我知道它是波兰语,但代码就是代码:) 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") 但我认为很容易根据您的要求进行更改

I done this while time ago.
On my blog You have solution, I know its in polish but code is code :) http://blog.grabek-adam.pl/2010/06/wordpress-popularne-posty-plugin-do-obrazkw/

Here You have just query:

$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
                        ");

This code will only include categories that You specify in here AND wp_term_taxonomy.term_id IN ("cat_id1,cat_id2,cat_id5") but i think it will be easy to change for Your requirements

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文