WordPress:列出没有评论的帖子

     2023-02-21     273

关键词:

【中文标题】WordPress:列出没有评论的帖子【英文标题】:WordPress: List posts with no comments 【发布时间】:2011-06-17 07:34:06 【问题描述】:

我有一个简单的页面,我想显示尚未评论的帖子列表。我该怎么做?我认为这是我可以添加到 query_posts 的一些参数?谢谢。

【问题讨论】:

【参考方案1】:

简单到:

query_posts( array ( 'post_type' => 'yourposttype', 'posts_per_page' => 10, 'comment_count' => 0, ) );

【讨论】:

【参考方案2】:

您可以设置过滤器和查询变量来修改查询帖子的 SQL。将此添加到您主题的 functions.php 文件中

function filter_comment_count( $sql )
    global $wpdb;
    $comment_count = get_query_var( 'comment_count' );

    if( is_numeric($comment_count) )
        $sql .= $wpdb->prepare( " AND $wpdb->posts.comment_count = %d ", $comment_count );

    return $sql;

然后您可以拨打query_posts( 'comment_count=0' );(任何号码),您只需事先添加过滤器,

add_filter( 'posts_where', 'filter_comment_count'  );

在您拨打电话后,您可能还想删除过滤器。

remove_filter( 'posts_where', 'filter_comment_count' ); 

【讨论】:

是的,你能做什么?谢谢:) 太棒了。这有助于解决使用 $sql .= $wpdb->prepare( " AND $wpdb->posts.comment_status = 'open' "); 启用 cmets 查找帖子的问题。而是。【参考方案3】:

很遗憾,query_posts 不允许您将查询限制为comment_count=0。你可以这样做:

query_posts( 'orderby=comment_count&order=ASC' );

但这并不显示 cmets 为零的帖子,它只显示 cmets 为零的帖子first

涉及更多(但更好)的解决方案是使用自定义查询,该查询专门将查询限制为具有 0 cmets 的帖子,但这意味着您必须创建自己的循环结构(至少 so far as I can tell)

global $wpdb;
$query = "
  SELECT *
  FROM $wpdb->prefixposts
  WHERE
  $wpdb->prefixposts.post_type = 'post'
  AND $wpdb->prefixposts.post_status = 'publish'
  AND $wpdb->prefixposts.comment_count = 0
  ORDER BY $wpdb->prefixposts.post_date
  DESC;
";

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

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

 <div class="post" id="post-<?php the_ID(); ?>">
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    <div class="entry">
       <?php the_content('Read the rest of this entry »'); ?>
    </div>
    <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  
    <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
 </div>
 <?php endforeach; ?>
 <?php else : ?>
    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php include (TEMPLATEPATH . "/searchform.php"); ?>
 <?php endif; ?>

这似乎在您的知识范围内实施吗?

【讨论】:

我有一些使用 SQL 和原始 PHP 的经验,所以我想我可以理解它。我只是玩玩,看看我能不能让它工作。感谢您的精彩回答。

在 WordPress 中列出与当前帖子具有相同标签的帖子

】在WordPress中列出与当前帖子具有相同标签的帖子【英文标题】:ListpostswiththesametagofcurrentpostinWordPress【发布时间】:2018-12-1511:59:22【问题描述】:我有一个有500000个帖子的网站,而且速度很慢。在WordPress网站上每个帖子的底部... 查看详情

带有年、月、日、帖子和评论计数的 Wordpress 存档列表

】带有年、月、日、帖子和评论计数的Wordpress存档列表【英文标题】:Wordpressarchivelistwithyear,month,days,postsandcommentcounts【发布时间】:2013-05-0420:56:21【问题描述】:我愿意实现这样的Wordpress存档列表:20135月(2)04-我喜欢Wordpress(3cm... 查看详情

分页不适用于列出 wordpress 的所有帖子

】分页不适用于列出wordpress的所有帖子【英文标题】:Paginationnotworkingforallpostslistingwordpress【发布时间】:2021-12-2707:02:36【问题描述】:我正在尝试获取wordpresswp-admin中的所有帖子,分页不起作用带有wp_query功能page仅显示分页链... 查看详情

Wordpress - 存档页面:按年份列出帖子

】Wordpress-存档页面:按年份列出帖子【英文标题】:Wordpress-archivepage:listpostsbyyear【发布时间】:2014-02-1617:45:30【问题描述】:我需要这样的东西:2014发布1发布2发布3发布42013发布1发布2发布3我怎样才能做到这一点?【问题讨论... 查看详情

wordpress3自定义帖子类型:如何获得评论(代码片段)

...了注释。我怎样才能获得评论呢?答案请参阅http://codex.wordpress.org/Function_Reference/register_post_type中的'supports'参数或者:http://codex.wordpress.org/Function_Reference/add_post_type_support例如:add_post_type_support('texts','comments');我的示例代码,... 查看详情

相对于所有帖子的 WordPress 人气得分(得分视图、分享和评论)

】相对于所有帖子的WordPress人气得分(得分视图、分享和评论)【英文标题】:WordPressPopularityScorerelativetoallposts(scoreviews,sharesandcomments)【发布时间】:2013-05-1315:20:31【问题描述】:我正在尝试找到一种解决方案来对我的帖子进行... 查看详情

按评论列出的热门帖子

$pop=$wpdb->get_results("SELECTid,post_title,comment_countFROM$wpdb->prefixpostsWHEREpost_type='post'ORDERBYcomment_countDESCLIMIT10"); foreach($popas$post):?><li><?phpecho$post->post_title;?></li><?phpendforeach;?> 查看详情

php按类别列出wordpress帖子(代码片段)

查看详情

php在wordpress中按类别列出帖子(代码片段)

查看详情

php从帖子和页面中删除wordpress评论。(代码片段)

查看详情

如何查询我的 WordPress 博客以列出我的 Vue.js 网站上的最新帖子?

】如何查询我的WordPress博客以列出我的Vue.js网站上的最新帖子?【英文标题】:HowdoIquerymyWordPressblogtolistmostrecentpostsonmyVue.jswebsite?【发布时间】:2020-12-1210:20:59【问题描述】:我正在尝试从我的网站获取我最近发布的帖子,但无... 查看详情

textwordpress列出所有帖子在wordpress(博客或新闻页面))(代码片段)

查看详情

Symfony2 - 列出从 Post 实体按 DESC 顺序排序的评论

】Symfony2-列出从Post实体按DESC顺序排序的评论【英文标题】:Symfony2-ListingCommentssortedinDESCorderfromPostentity【发布时间】:2014-08-0419:07:38【问题描述】:我将评论设置为多对一/一对多到帖子。我想以DESC顺序显示帖子中的所有评论。... 查看详情

在非wordpress网站上列出最近的帖子

ListyourWordPresspostsonanon-WordPresswebsite<?php$how_many=1;//howmanypoststodisplayrequire('blog1/wp-config.php');//thepathtothewp-configfileoftheblogIwanttouse$news=$wpdb->get_results("SELECT'ID','post_title','post_content'FROM$wpdb->postsWHERE'post_type'="post"AND'post_st... 查看详情

如何在 Gatsby 中获取所有 Wordpress 帖子和显示标签

】如何在Gatsby中获取所有Wordpress帖子和显示标签【英文标题】:HowtogetallWordpresspostsanddisplaytagsinGatsby【发布时间】:2020-07-3118:12:42【问题描述】:第一次使用Gatbsy和GraphQL。我想知道如何显示标签。GraphQL编辑器中没有以明显的方... 查看详情

wordpress-显示2009年评论最多的帖子

FromWebDesignLedger"13UsefulCodeSnippetsforWordPressDevelopment"Foradditionalinformation,seethelinkabove.<ul><?php$result=$wpdb->get_results("SELECTcomment_count,ID,post_title,post_dateFROM$wpdb->postsWHEREpost_dateBETWEEN'2009-01-01'AND'2009-12-31'ORDERBYcomment_co... 查看详情

Wordpress - 计算每年的帖子

】Wordpress-计算每年的帖子【英文标题】:Wordpress-Countpostsforeachyear【发布时间】:2015-02-1907:20:46【问题描述】:我正在使用以下代码列出我博客中的所有年份,我想我们可以称之为存档。<?php$years=$wpdb->get_col("SELECTDISTINCTYEAR(po... 查看详情

优化 WordPress 插件“更好的 WordPress 最近评论”中的慢查询

】优化WordPress插件“更好的WordPress最近评论”中的慢查询【英文标题】:OptimizeslowqueryinWordPressplugin"BetterWordPressRecentComments"【发布时间】:2015-12-3103:36:44【问题描述】:我正在优化针对MySQL的查询,我的慢查询日志显示Word... 查看详情