Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress-显示上周阅读最多的帖子_Php_Wordpress_Time_Count_Posts - Fatal编程技术网

Php WordPress-显示上周阅读最多的帖子

Php WordPress-显示上周阅读最多的帖子,php,wordpress,time,count,posts,Php,Wordpress,Time,Count,Posts,如何显示上周的大多数阅读帖子 $options = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $limit, 'ignore_sticky_posts' => true, /* 'orderby' => 'meta_value_num', */ 'or

如何显示上周的大多数阅读帖子

    $options = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => $limit,
        'ignore_sticky_posts' => true,
        /* 'orderby' => 'meta_value_num', */
        'orderby' => 'rand',
        'order' => 'desc',
        'meta_key' => 'post_views_count'
    );
我有下面的代码来记录每一篇文章的点击率,这样我就知道有多少人阅读了这篇文章

    function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}

function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}


// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
/**
 * Add a new column in the wp-admin posts list
 *
 * @param $defaults
 *
 * @return mixed
 */

function subh_posts_column_views( $defaults ) {
    $defaults['post_views'] = __( 'Views' );
    return $defaults;
}

/**
 * Display the number of views for each posts
 *
 * @param $column_name
 * @param $id
 *
 * @return void simply echo out the number of views
 */

function subh_posts_custom_column_views( $column_name, $id ) {
    if ( $column_name === 'post_views' ) {
       echo getPostViews( get_the_ID() );
    }
}

add_filter( 'manage_posts_columns', 'subh_posts_column_views' );
add_action( 'manage_posts_custom_column', 'subh_posts_custom_column_views', 5, 2 );
如何设置以下内容以显示上周的大多数阅读帖子

    $options = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => $limit,
        'ignore_sticky_posts' => true,
        /* 'orderby' => 'meta_value_num', */
        'orderby' => 'rand',
        'order' => 'desc',
        'meta_key' => 'post_views_count'
    );

您可以为
wp\u postmeta
表中的每篇文章添加一个
'last\u view\u date'
元数据,稍微更改前两个函数,并为上周(过去7天)阅读的文章创建一个返回true的条件函数

以下是我定制的两个首个函数:

function getPostViews($postID){
    $count_key = 'post_views_count';
    $date_key = 'last_view_date';
    $today_date = date("Y-m-d");
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        // Note: update_post_meta() create the data if not exist
        update_post_meta($postID, $count_key, '0');

        // Setting the "view date" for the first time
        update_post_meta($postID, $date_key, $today_date);

        return '0 View';
    }
    return $count.' Views';
}

function setPostViews($postID) {
    $count_key = 'post_views_count';
    $date_key = 'last_view_date';
    $today_date = date("Y-m-d");
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        // Note: update_post_meta() create the data if not exist
        update_post_meta($postID, $count_key, '0');

        // Setting the "view date" for the first time
        update_post_meta($postID, $date_key, $today_date);

    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);

        // Updating the "view date"
        update_post_meta($postID, $date_key, $today_date);
    }
}
这是条件函数代码,用于过滤上周查看的帖子:

// Conditional function that return true, whem a post have been read at least once in last week.
function is_week_viewed($postID) {
    $now = time();
    $date_key = 'last_view_date';
    $view_date = get_post_meta($postID, $date_key, true);
    $viewdate = strtotime( $view_date );
    $datediff = $now - $viewdate;
    $days = floor($datediff/(60*60*24));
    $count = get_post_meta($postID, $count_key, true);
    if ( $count > '0' && $days < 8) {
        return true;
    }
}
//返回true的条件函数,上周至少读取一次帖子。
功能已在周内查看($postID){
$now=时间();
$date_key='last_view_date';
$view\u date=get\u post\u meta($posted,$date\u key,true);
$viewdate=STROTIME($view\U date);
$datediff=$now-$viewdate;
$days=下限($datediff/(60*60*24));
$count=get\u post\u meta($posted,$count\u key,true);
如果($count>'0'&&$days<8){
返回true;
}
}
有了这些材料,你就可以在上周的搜索查询中显示大多数阅读文章

然后使用
is\u week\u viewsed()
条件函数,您现在可以筛选搜索结果,以仅获取上周的帖子

引用-条件函数基于此旧线程:

非常感谢我!它工作得很好!在我标记它之前,我想在现场检查一些东西。