• Hello,
    How to display most read posts in the last week?

    I have the following code to record every hit of the posts, so i would know how many people read the post.

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

    How can i set the following

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

    to show most read posts in the last week?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘How to display most read posts in the last week?’ is closed to new replies.