过去24小时、每周、每月、每年和所有时间的评论最多-在哪里

时间:2013-10-30 作者:Alx

我已经在这里删除了我的旧帖子,因为posts\\u的方法已经过时了。

多亏了kaiser,现在在一定的时间范围内列出大多数评论帖子是多么容易:

<?php
$popular = new WP_Query( array(
    \'post_type\'             => array( \'post\' ),
    \'showposts\'             => 6,
    \'cat\'                   => \'MyCategory\',
    \'ignore_sticky_posts\'   => true,
    \'orderby\'               => \'comment_count\',
    \'order\'                 => \'dsc\',
    \'date_query\' => array(
        array(
            \'after\' => \'1 week ago\',
        ),
    ),
) );
?>
<?php while ( $popular->have_posts() ): $popular->the_post(); ?>
    <?php the_title(); ?>
<?php endwhile; ?>
注意:这按评论最多的排序articles posted within X date, 不检查所有文章,然后在所有文章中查找在此时间范围内最多的评论。不知道怎么做第二个,也不知道它有多重。

1 个回复
最合适的回答,由SO网友:kaiser 整理而成

在哪里

TheWP_Comment_Query 类的子句具有以下筛选器:

$pieces = array( \'fields\', \'join\', \'where\', \'orderby\', \'order\', \'limits\', \'groupby\' );
$clauses = apply_filters_ref_array( \'comments_clauses\', array( compact( $pieces ), &$this ) );
那意味着你可以comments_clauses 并过滤$clauses[\'where\'] 部分

怎么做

自WP 3.7以来,我们得到了WP_Date_Query 班其工作原理如下(简化示例):

期间使用的默认值WP_Query - 通过查询变量检索:

array(
    \'year\'     => \'\',
    \'monthnum\' => \'\',
    \'week\'     => \'\',
    \'day\'      => \'\',
    \'hour\'     => \'\',
    \'minute\'   => \'\',
    \'second\'   => \'\',
)
如何进行date_query: 复制自的代码示例here

$dateQuery = new WP_Query( array(
    \'date_query\' => array(
        \'column\'   => \'optional, column to query against, default is post_date\',
        \'compare\'  => \'optional, see WP_Date_Query::get_compare()\',
        \'relation\' => \'optional, OR or AND, how the sub-arrays should be compared, default is AND\',
        array(
            \'column\'    => \'see above\',
            \'compare\'   => \'see above\',
            \'after\'     => \'string or array, see WP_Date_Query::build_mysql_datetime()\',
            \'before\'    => \'string or array, see WP_Date_Query::build_mysql_datetime()\',
            \'inclusive\' => \'boolean, for after/before, whether exact value should be matched or not\',
            \'year\'      => \'4 digit int\',
            \'month\'     => \'int, 1-12\',
            \'week\'      => \'int, 0-53\',
            \'day\'       => \'int, 1-31\',
            \'hour\'      => \'int, 0-23\',
            \'minute\'    => \'int, 0-60\',
            \'second\'    => \'int, 0-60\',
        ),
    ),
    // ... other query args
) );
你也可以做一个简单的WP_Date_Query() 并获取生成的SQL字符串,以便稍后在子句筛选器中使用:

$dateQuery = WP_Date_Query( array( /* your query */ ) );
$whereDate = $dateQuery->get_sql();

// Inspect result
var_dump( $whereDate );
现在,相同来源的帖子及其评论的日期查询:

// All comments that are within the past week
$some_comments = get_comments( array(
    \'post_ID\'    => get_the_ID(), // Can be omited as well
    \'date_query\' => array(
        array(
            \'after\' => \'1 week ago\',
        ),
    ),
    \'count\' => true // return only the comment count
) );
模型未测试。。。

global $wp_query;
$daysQuery = new WP_Query( array(
    \'post_type\'           => array( \'post\' ),
    \'showposts\'           => 6,
    \'cat\'                 => \'mycat\',
    \'ignore_sticky_posts\' => true,
    \'orderby\'             => \'comment_count date\',
    \'date_query\' => array(
        array(
            \'after\'  => array(
                \'year\'  => date( "Y" ),
                \'month\' => date( "m" ),
                \'day\'   => "01",
            ),
            \'before\' => array(
                \'year\'  => date( "Y" ),
                \'month\' => date( "m", strtotime( "-1 Months" ) ),
                \'day\'   => date( "t", strtotime( "-1 Months" ) ),
            ),
            // \'inclusive\' => true,
            \'month\'     => date( "m" ),
        ),
    ),
) );

结束

相关推荐

customize footer widgets area

我有一个自定义的页脚小部件区域,它在一行中水平显示最多4个小部件。如果我添加了4个以上的小部件,那么布局就会中断,因为我试图在同一行中显示它。我想让它更灵活,例如有2行(div),我可以在第一行中添加let’s 2 widget,在第二行中添加4个widget。可能我需要的是复制这一个,并制作两个页脚区域。这可能吗?如果可能,我如何实现?下面是我的小部件的实际代码。php: /* Footer Widgets */ $footer_widgets_num = wp_get_sidebars_