D
D
ddem72017-09-26 22:29:19
SQL
ddem7, 2017-09-26 22:29:19

How to exclude re-counting the number of views per IP for popular WordPress posts?

Hello. Please help me figure it out. How to exclude re-counting the number of views for popular posts from the same IP, i.e. if a person from this IP has looked at an article, then the calculation should no longer be carried out upon re-entry.
And another question is what needs to be corrected in the code for compatibility with caching plugins? In one article ( https://wpcafe.org/tutorials/kak-otobrazhat-populy... ), for compatibility with the caching plugin, the author writes the following solution, but I did not understand what exactly it changes, namely:
"We using the W3 Total Cache plugin, it has an option called Fragmented Caching, you can also use this plugin and this option to make everything work properly, here is what you need to change:
I use the following solution bloggood.ru/wordpress/kak-vyvesti-populyarnye-zapi...
in functions.php
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 views";
}
return ' Views: '.$count;
}
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);
}
}
in single.php to collect page view information
<?php setPostViews(get_the_ID()); ?>
Display the number of views


  • <?php
    $args = array( 'posts_per_page' => 5, 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' );
    query_posts($args);
    while ( have_posts() ) : the_post();
    ?>

  • <?php the_title(); ?>
    <?php endwhile; wp_reset_query(); ?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
Lazy @BojackHorseman, 2019-06-15
@dimas199862

SELECT *
FROM `messages` m 
JOIN `users` u1 ON u1.`id` = m.`fromid`
JOIN `users` u2 ON u2.`id` = m.`toid`

T
tyzberd, 2017-09-26
@tyzberd

do a check if there are no cookies call wpb_set_post_views(get_the_ID());
then set cookies.
as I understand it, if with a caching plugin, then instead of wpb_set_post_views ($ post_id); write
this is a dynamic block https://odd.blog/2013/05/01/mfunc-in-wp-super-cach...

D
ddem7, 2017-09-27
@ddem7

Delving into this topic, I read that the counter of unique views through Cookies should not be done for many reasons
https://ru.stackoverflow.com/questions/454939/%D0%...
But then how else can you implement the counter of unique views so that it can be was to link to this decision bloggood.ru/wordpress/kak-vyvesti-populyarnye-zapi...
Read www.cyberforum.ru/php-beginners/thread1626274.html, but even more questions. What is the best solution?
I will be grateful for advice!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question