WordPress自定义查询 - 每天只显示作者的一篇最新帖子

时间:2015-04-01 21:39:34

标签: wordpress

我有一个页面显示所有已发布的帖子,我想添加过滤器,在同一天由同一作者过滤掉多个帖子。

因此,如果一位作者在同一天写了3篇帖子,则只显示该作者的最新帖子。将相同的规则应用于网站中的所有作者。

我怎样才能做到这一点?

所以我发现了这一点,但仍不确定如何获得用户撰写帖子的所有日期。

add_action( 'post_creation_limits_custom_checks', 'post_per_day_limit' );

function post_per_day_limit( $type, $user_id ) {
    global $bapl,$wpdb;

    // safe check: Plugin installed?
    ! isset( $bapl ) AND _doing_it_wrong( __FUNCTION__, sprintf( 'You need to %sinstall the needed Plugin%s', '<a href="http://wordpress.org/extend/plugins/bainternet-posts-creation-limits/">', '</a>' ), 0 );

    $time_in_days = 1; // 1 means in last day
    $count = $wpdb->get_var(
        $wpdb->prepare("
            SELECT COUNT(*) 
            FROM $wpdb->posts 
            WHERE post_status = 'publish' 
            AND post_type = %s 
            AND post_author = %s
            AND post_date >= DATE_SUB(CURDATE(),INTERVAL %s DAY)",
            $type,
            $user_id,
            $time_in_days
        )
    );
    if ( 0 < $count ) 
        $count = number_format( $count );

    // here you can check since we have the $count ex: 
    // limit for 2 posts a day
    if ( 1 < $count ) {
        // return limit reached message using the plugin class
        exit( $bapl->bapl_not_allowed( 'you can not posts more them two posts a day' ) );
    }
    // else do nothing
}

0 个答案:

没有答案