Code Monkey home page Code Monkey logo

wp-postviews's Introduction

WP-PostViews

Contributors: GamerZ
Donate link: https://lesterchan.net/site/donation/
Tags: views, hits, counter, postviews
Requires at least: 4.0
Tested up to: 6.3
Stable tag: 1.78

Enables you to display how many times a post/page had been viewed.

Description

Usage

  1. Open wp-content/themes/<YOUR THEME NAME>/index.php
  2. You may place it in archive.php, single.php, post.php or page.php also.
  3. Find: <?php while (have_posts()) : the_post(); ?>
  4. Add Anywhere Below It (The Place You Want The Views To Show): <?php if(function_exists('the_views')) { the_views(); } ?>
  5. Or you can use the shortcode [views] or [views id="1"] (where 1 is the post ID) in a post
  6. Go to WP-Admin -> Settings -> PostViews to configure the plugin.

Development

https://github.com/lesterchan/wp-postviews/

Translations

http://dev.wp-plugins.org/browser/wp-postviews/i18n/

Credits

Donations

I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.

Changelog

Version 1.78

  • NEW: Add %POST_THUMBNAIL_URL% to template variables

Version 1.77

  • NEW: Use Vanilla JS. Props @JiveDig
  • NEW: Bump to WordPress 6.2
  • NEW: Support views under fields for Rest API. Props @vitro-mod

Version 1.76.1

  • NEW: Add Post Author in views template
  • NEW: Bump for WordPress 5.3

Version 1.76

  • NEW: Added postviews_should_count filter
  • FIXED: Change to (int) from intval() and use sanitize_key() with it.

Version 1.75

  • NEW: Use WP_Query() for most/least viewed posts

Version 1.74

  • NEW: Bump WordPress 4.7
  • NEW: Template variable %POST_CATEGORY_ID%. It returns Post's Category ID. If you are using Yoast SEO Plugin, it will return the priority Category ID. Props @FunFrog-BY

Version 1.73

  • FIXED: In preview mode, don't count views

Version 1.72

  • NEW: Add %POST_THUMBNAIL% to template variables

Version 1.71

  • FIXED: Notices in Widget Constructor for WordPress 4.3

Version 1.70

  • FIXED: Integration with WP-Stats

Version 1.69

  • NEW: Shortcode [views] or [views id="POST_ID"]` to embed view count into post
  • NEW: Added template variable %VIEW_COUNT_ROUNDED% to support rounded view count like 10.1k or 11.2M

Version 1.68

  • NEW: Added action hook 'postviews_increment_views' and 'postviews_increment_views_ajax'
  • NEW: Allow custom post type to be chosen under the widget

Version 1.67

  • NEW: Allow user to not use AJAX to update the views even though WP_CACHE is true

Version 1.66

  • NEW: Supports MultiSite Network Activation
  • NEW: Add %POST_DATE% and %POST_TIME% to template variables
  • NEW: Add China isearch engines bots
  • NEW: Ability to pass in an array of post types for get_most/least_*() functions. Props Leo Plaw.
  • FIXED: Moved uninstall to uninstall.php and hence fix missing nonce. Props Julio Potier.
  • FIXED: Notices and better way to get views from meta. Props daankortenbach.
  • FIXED: No longer needing add_post_meta() if update_post_meta() fails.

Version 1.65 (02-06-2013)

  • FIXED: Views not showing in WP-Admin if "Display Options" is not set to "Display to everyone"

Upgrade Notice

N/A

Screenshots

  1. PostViews
  2. Admin - PostViews Options

Frequently Asked Questions

How To View Stats With Widgets?

  • Go to WP-Admin -> Appearance -> Widgets
  • The widget name is Views.

To Display Least Viewed Posts

<?php if (function_exists('get_least_viewed')): ?>
	<ul>
		<?php get_least_viewed(); ?>
	</ul>
<?php endif; ?>
  • The first value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example array('post', 'page').
  • The second value you pass in is the maximum number of post you want to get.
  • Default: get_least_viewed('both', 10);

To Display Most Viewed Posts

<?php if (function_exists('get_most_viewed')): ?>
	<ul>
		<?php get_most_viewed(); ?>
	</ul>
<?php endif; ?>
  • The first value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example array('post', 'page').
  • The second value you pass in is the maximum number of post you want to get.
  • Default: get_most_viewed('both', 10);

To Display Least Viewed Posts By Tag

<?php if (function_exists('get_least_viewed_tag')): ?>
	<ul>
		<?php get_least_viewed_tag(); ?>
	</ul>
<?php endif; ?>
  • The first value you pass in is the tag id.
  • The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example array('post', 'page').
  • The third value you pass in is the maximum number of post you want to get.
  • Default: get_least_viewed_tag(1, 'both', 10);

To Display Most Viewed Posts By Tag

<?php if (function_exists('get_most_viewed_tag')): ?>
	<ul>
		<?php get_most_viewed_tag(); ?>
	</ul>
<?php endif; ?>
  • The first value you pass in is the tag id.
  • The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example array('post', 'page').
  • The third value you pass in is the maximum number of post you want to get.
  • Default: get_most_viewed_tag(1, 'both', 10);

To Display Least Viewed Posts For A Category

<?php if (function_exists('get_least_viewed_category')): ?>
	<ul>
		<?php get_least_viewed_category(); ?>
	</ul>
<?php endif; ?>
  • The first value you pass in is the category id.
  • The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example array('post', 'page').
  • The third value you pass in is the maximum number of post you want to get.
  • Default: get_least_viewed_category(1, 'both', 10);

To Display Most Viewed Posts For A Category

<?php if (function_exists('get_most_viewed_category')): ?>
	<ul>
		<?php get_most_viewed_category(); ?>
	</ul>
<?php endif; ?>
  • The first value you pass in is the category id.
  • The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example array('post', 'page').
  • The third value you pass in is the maximum number of post you want to get.
  • Default: get_most_viewed_category(1, 'both', 10);

To Sort Most/Least Viewed Posts

  • You can use: <?php query_posts( array( 'meta_key' => 'views', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); ?>
  • Or pass in the variables to the URL: http://yoursite.com/?v_sortby=views&v_orderby=desc
  • You can replace DESC with ASC if you want the least viewed posts.

To Display Updating View Count With LiteSpeed Cache

Use: <div id="postviews_lscwp"></div> to replace <?php if(function_exists('the_views')) { the_views(); } ?>. NOTE: The id can be changed, but the div id and the ajax function must match. Replace the ajax query in wp-content/plugins/wp-postviews/postviews-cache.js with

jQuery.ajax({
    type:"GET",
    url:viewsCacheL10n.admin_ajax_url,
    data:"postviews_id="+viewsCacheL10n.post_id+"&action=postviews",
    cache:!1,
    success:function(data) {
        if(data) {
            jQuery('#postviews_lscwp').html(data+' views');
        }
   }
});

Purge the cache to use the updated pages.

To Get Views With REST API

You can obtain the number of post views by adding views to your _fields parameter: /wp/v2/posts?_fields=views,title

wp-postviews's People

Contributors

daankortenbach avatar funfrog-by avatar kittsville avatar lesterchan avatar marcinkrzeminski avatar mayukojpn avatar pi-benoit avatar syamilmj avatar syshut avatar vitro-mod avatar xtrime-ru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

wp-postviews's Issues

How to use wp-postviews extention with Cache Enabler plugin?

I installed wp-postviews on site with Cache Enabler plugin activated.
Using standard, not async code provided in Readme
caused common problem with not updating current views (as I expected - this page is rendered and written to cache).
The only way for now is to clear cache manually and decrease cache lifetime.
But I can see that you provided some async code using JS and LiteSpeed Cache:
jQuery.ajax({
type:"GET",
url:viewsCacheL10n.admin_ajax_url,
data:"postviews_id="+viewsCacheL10n.post_id+"&action=postviews",
cache:!1,
success:function(data) {
if(data) {
jQuery('#postviews_lscwp').html(data+' views');
}
}
});

but this code closely connected with LiteSpeed Cache plugin params.
I need more universal solution, that I can use with Cache Enabler plugin.
I will appreciate if you adivse me some solution or snippet.

Reset page/post views

Hello :-)

When copying a page or post to create a new design it also copies over the page views from the original post/page. How do you reset the view count for a page/post?

Thank you for the great work.

Incrementation not working for mobile

Hello,

I'm running into an issue with the plugin on the counter.
When I refresh a post 10 times on my desktop, it works like a charm, my post has 10 more views. But when i'm accessing to the same post on my phone, nothing happens.

There's no cache plugin installed.

Thank you for your help !

custom orderby meta wp_query "fix"

I post this here because I had issues with a custom loop ordered by views. It didn't show posts that didn't have the views meta_key.

Run this once in your functions.php and remove after.. or let it sit there but there really is no need for it.

This code adds a meta_key views if the post doesn't have one and fills the value with a 0.

add_action( 'init', 'post_views_default' );

function post_views_default() {

    if ( get_option( 'post_views_setup_has_run' ) )
        return;

    $args = array(
        'posts_per_page' => -1,
        'fields' => 'ids',
        'post_type' => array( 'post', 'page' )
    );
    $viewsToZero = new WP_Query( $args );

    foreach ( $viewsToZero->posts as $post ){

        if ( ! get_post_meta( $post->ID, 'views', true ) )
            add_post_meta( $post->ID, 'views', 0, true );
    }

    add_option( 'post_views_setup_has_run', 'yes', $deprecated = '', $autoload = 'yes' );
}

Why "Views" Column in back-end is not sortable ?

I don't have the options to sort the views columns on my custom post type.
Should I do something special to enabled it ?

I found the sort_postviews_column function but can't get it working.

Thanks for your help

Create a Draft based on an existent Post/Page

Hi, first of all, thanks for this plugin.
It´s amazing and works very fine.
I realized, recently (probably since all), that when I copy an existent post as a draft to save time in edition, the came with the same views as the original one.

Did you already have the same experience?

Thanks

Edson Sobreira

image

Jetpack WP Stats Integration

I can see in the change log that you mention a fix for WP Stats integration. I've installed WP Postviews into a website that is integrated with Jetpack Stats (which I assume is the WP Stats mentioned in the fix). The view counts shown by the [views] shortcode do not appear to reflect historical WP Stats data. Have I misunderstood the plugin's features or do I need to alter a setting?

Backup view counts before reinstall WordPress

Sir,
First of all thanks for the plugin.
I need to reinstall my WP blog site. Is any option to store and restore view counts? I took backup my site without plugin backup. just took post, page and database backup. So I'm scared about loss the view counts. Can you give me any suggestion please?

pt_BR translation

I've translated the plugin into Brazilian portuguese, I do not know if this is the right place to post since I'm kinda new here, I don't know how to attach a file either so I've uploaded a zip file containing the wp-postviews-pt_BR.mo and wp-postviews-pt_BR.po into depositfiles

[http://depositfiles.com/files/wsgasawid]

help about smof category

I want to display posts from two different categories in WordPress. The categories should have their own styles. But I can't make it work properly. The post appears but not in date order or limited to five posts.

Problem wirh W3TC database cache

I am having problem using this plugin with w3tc
the views are not updated every time the page loads (though its logged)
how to solve this?

Insert icon to count

Hi

I love this plugin and was hoping you could show me how to insert a "class" so I can insert an icon like example an eye before the count number.
Thanks

Posts with over 100 views showing before the ones with less than 100 in the query (desc. order)

I put in the "Dynamic OrderBy Field" the "view" field, and it is showing the proper posts in order of most accessed, to least accessed. But it does not show the posts with view count over 100 — and i guess they are in another place of the query, because i tested to show the posts in a category that has only 3 posts, one with 50 views, and other two with views over 100, and the one with 50 views showed up first, but the other two are in the correct order of most views. Anyone know how to arrange the query correctly and show the pages with over 100 views first?

Count by IP?

Any way to count by IP? I'm not interested in views being counted on refresh.

Cheers.

WP-JSON API Supported ?

  1. Is "wp-postviews" support +1 view when get post data from wp-json api ?
  2. Also, get this count in wp-json api

Google Analytics

What do you think about adding support for Google Analytics?

GA usually gives more accurate data of page views. We can add an option for users to retrieve data from GA instead of relying on the plugin's manual implementation.

I know there's already several plugin that does exactly this but the implementation is mostly too heavy and have unrealistic use case.

Problem for pluralization

When there is only 1 visit show the plural label "visits" insted of "visit"

For example:

1 visits.
2 visits.

Undefined variable: should_count in wp-content\plugins\wp-postviews\wp-postviews.php on line 118

  1. Enable xdebug for php as usual, zend_extension_ts=c:\php-tools\php\ext\php_xdebug-2.1.0RC1-5.2-vc6.dll
  2. Enable wordpress debug define('WP_DEBUG', true); in wp-config
    That will cause php notice messages to be displayed.

Create custom page template with the_loop which shows only one page. Create a page basing on the template and try to see it. You'll get

[15-Sep-2013 11:49:43] PHP Notice: Undefined variable: should_count in C:\php-tools\wp-novostroyka\wp-content\plugins\wp-postviews\wp-postviews.php on line 118
[15-Sep-2013 11:49:43] PHP Stack trace:
[15-Sep-2013 11:49:43] PHP 1. {main}() C:\php-tools\wp-novostroyka\index.php:0
[15-Sep-2013 11:49:43] PHP 2. require() C:\php-tools\wp-novostroyka\index.php:17
[15-Sep-2013 11:49:43] PHP 3. require_once() C:\php-tools\wp-novostroyka\wp-blog-header.php:16
[15-Sep-2013 11:49:43] PHP 4. include() C:\php-tools\wp-novostroyka\wp-includes\template-loader.php:47
[15-Sep-2013 11:49:43] PHP 5. get_header() C:\php-tools\wp-novostroyka\wp-content\themes\novostroyka\page-templates\free-adv.php:11
[15-Sep-2013 11:49:43] PHP 6. locate_template() C:\php-tools\wp-novostroyka\wp-includes\general-template.php:35
[15-Sep-2013 11:49:43] PHP 7. load_template() C:\php-tools\wp-novostroyka\wp-includes\template.php:383
[15-Sep-2013 11:49:43] PHP 8. require_once() C:\php-tools\wp-novostroyka\wp-includes\template.php:407
[15-Sep-2013 11:49:43] PHP 9. wp_head() C:\php-tools\wp-novostroyka\wp-content\themes\novostroyka\header.php:21
[15-Sep-2013 11:49:43] PHP 10. do_action() C:\php-tools\wp-novostroyka\wp-includes\general-template.php:1607
[15-Sep-2013 11:49:43] PHP 11. call_user_func_array() C:\php-tools\wp-novostroyka\wp-includes\plugin.php:406
[15-Sep-2013 11:49:43] PHP 12. wp_enqueue_scripts() C:\php-tools\wp-novostroyka\wp-includes\plugin.php:0
[15-Sep-2013 11:49:43] PHP 13. do_action() C:\php-tools\wp-novostroyka\wp-includes\script-loader.php:806
[15-Sep-2013 11:49:43] PHP 14. call_user_func_array() C:\php-tools\wp-novostroyka\wp-includes\plugin.php:406
[15-Sep-2013 11:49:43] PHP 15. wp_postview_cache_count_enqueue() C:\php-tools\wp-novostroyka\wp-includes\plugin.php:0

I understand that PHP allows you to skip a variable definition, but being a Java developer I always define a variable and initialize it with default meaningful value, and I think it's a good practice to keep your code cleared from errors and unwanted messages.

I'd fix that with following:

function wp_postview_cache_count_enqueue() {
global $user_ID, $post;
if (!wp_is_post_revision($post) && (is_single() || is_page())) {
$views_options = get_option('views_options');

   $should_count = false;

Thanks.

To display the views of a separate post

I saw this link and succeeded. How to display by popular post.
However, it failed to display the number of views of a separate posts.
Page views are displayed, not separate post views.
(I use a translator because I can't speak English. I'm sorry)
캡처

Unique visits

Is it possible to count only unique visit form registered users? I need to know how many registered users viewed my post, so I need to count only their first visit. Is there any way to do this? Thanks.

Is Not Updating

on my site the views are not updating in live
the update post view sometimes it not displaying the right amount of view

Is it useful on WordPress 5.7.1

Recently , I updated wordpres to 5.7.1, and I found the postview is not useful.The count is not changing now.
Is it useful on wp5.7.1,or I just used it wrong.

Fix for working with cached pages

Hi there, we've been using your WP Postviews plugin for a while with one of our clients and found the need to alter the plugin for proper counts when the pages are cached. You may consider including the diffs in the patch here:
http://drop.bigdawggi.com/Hq4F

Enqueing the scripts this way also allows greater flexibility for other plugins to modify when the JS is included (i.e., for use with script concatenation plugins)

Since we've kept a mirror of the SVN plugin for a while at here, forking and issuing a pull request would be a little cumbersome, so you can just apply the few changes as needed from the patch above.

Glad to see you moving to Github!

Views doesn't update

I have installed a plugin, added php line into single.php, but it still doesn't work. I have cache plugin but it still should work if I turn off Use AJAX To Update Views right?

Want to call all Post View in another plugin

Hi, my name is Galang you can call me Mz, i want to integrated your plugin, in my first plugin wordpress, can you explain me what should i do if i want to count all view in my all post depend on the user id, if u have an email address please contact me in [email protected]

post view not showing

Hi
After updating your plugin to last version when i want to see all my posts i get this error
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘sort_postviews_column ‘ not found or invalid function name in /home1/sourcekh/public_html/learn/wp-includes/class-wp-hook.php on line 286
please help fast

FR - Use vanilla JS (not jQuery) loaded in footer

This plugin seems useful, thanks! However, I can't use it because we don't want jQuery loading on our sites. I'm just making an official feature request if you could switch to fetch in JS instead of ajax in jQuery :)

Post views by Day/Week/Month

Hello,

I use your extension long time. But I would like to extend it .

I want show post by views with interval of time day/week/month.

I have found ressource like this (http://wordpress.org/extend/plugins/wordpress-popular-posts) :

function wpp_get_views($id = NULL, $range = NULL, $number_format = true) {

    // have we got an id?
    if ( empty($id) || is_null($id) || !is_numeric($id) ) {
        return "-1";
    } else {
        global $wpdb;

        $table_name = $wpdb->prefix . "popularposts";

        if ( !$range || 'all' == $range ) {
            $query = "SELECT pageviews FROM {$table_name}data WHERE postid = '{$id}'";
        } else {
            $interval = "";

            switch( $range ){
                case "yesterday":
                    $interval = "1 DAY";
                break;

                case "daily":
                    $interval = "1 DAY";
                break;

                case "weekly":
                    $interval = "1 WEEK";
                break;

                case "monthly":
                    $interval = "1 MONTH";
                break;

                default:
                    $interval = "1 DAY";
                break;
            }

            $now = current_time('mysql');

            $query = "SELECT SUM(pageviews) FROM {$table_name}summary WHERE postid = '{$id}' AND last_viewed > DATE_SUB('{$now}', INTERVAL {$interval}) LIMIT 1;";
        }

        $result = $wpdb->get_var($query);

        if ( !$result ) {
            return "0";
        }

        return ($number_format) ? number_format_i18n( intval($result) ) : $result;
    }

}

Someone can help me?

Sticky post should be exclude from the Widget

In current version, if there is a sticky post, it will include in all queries such as most_viewed. Then it will show $limit + 1 records, and the sticky post is the first one. I think it's not the expected result in most cases. So would it be better to add a condition ignore_sticky_posts in WP_Query?

wordpress 5.0.1 warning

When I install 1.7.6 on wordpress 5.0.1.There are two line Warning message in the admin's article page:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'sort_postviews_column ' not found or invalid function name in /www/wwwroot/wordpress/wp-includes/class-wp-hook.php on line 286

Warning: Invalid argument supplied for foreach() in /www/wwwroot/wordpress/wp-admin/includes/class-wp-list-table.php on line 967

And when I disable the wp-postviews , this warning message disappear

Error when WP_CACHE isn't defined

" Notice: Undefined index: views_use_ajax in /srv/www/wp/wp-content/plugins/wp-postviews/postviews-options.php on line 26"

Around line 90 you have the following to display a specific field (use_ajax):

... view_option['use_ajax'] ... yada yada

So when you are checking the submit data and setting view_options you should probably exclude views_use_ajax from the array, and then include the view option under the same constraints:

if( defined( 'WP_CACHE' ) && WP_CACHE )
$views_options['use_ajax'] = intval( $_POST['views_use_ajax'] );

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.