Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Display Posts by Views (Popular Posts Widget)

It could be fix using session, try following.

Initialize session using following code in your functions.php.

    function init_sessions() {
    if (!session_id()) {
        session_start();
    }
}
add_action('init', 'init_sessions');

add a function for setting session

function set_session_posts_viewed($postID)
{
    if(empty($_SESSION['posts_viewed']))
    {
        $posts_viewed = array($postID);
        $_SESSION['posts_viewed'] = $posts_viewed;
    }
    else
    {
        $posts_viewed = $_SESSION['posts_viewed'];
        $posts_viewed[] = $postID;
        $_SESSION['posts_viewed'] = $posts_viewed;
    }
}

change post count updating code like this

function observePostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if(!isset($_SESSION['posts_viewed']) || (isset($_SESSION['posts_viewed']) && !in_array($postID, $_SESSION['posts_viewed'])))
    {
        if($count==''){
            $count = 0;
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
            set_session_posts_viewed($postID);
        }else{
            $count++;
            update_post_meta($postID, $count_key, $count);
            set_session_posts_viewed($postID);
        }
    }
}

Hope it fix the issue. Let me know if you have trouble.

Related Posts:

  1. WordPress widget new instance creates content duplicates
  2. How to check widget-cpt meta and display its custom css in the head?
  3. Programmatically add widgets to sidebars
  4. WordPress 3.8 – Dashboard 1 Column Screen Options
  5. Executing Javascript When a Widget is Added in the Backend
  6. Loading scripts only if a particular shortcode or widget is present
  7. Limit number of Widgets in Sidebars
  8. How Can I Add the “Insert From URL” Tab to a Custom 3.5 Media Uploader?
  9. Give Editor Access To Sidebar
  10. How to load Widget javascript + css files only if used?
  11. Where is the content of widgets stored in mysql table
  12. Hide specific categories from category widget
  13. changing wp-admin/widgets.php
  14. Add class to before_widget from within a custom widget
  15. How can I use the built in WordPress “browse link” functionality?
  16. Is there any way to dynamically alter widget titles?
  17. Translate widget titles using qTranslate plugin
  18. Code for Recent Posts Widget
  19. Get number of widgets in sidebar
  20. Text Widget creates a
  21. Can a widget in the Customizer be “single-use” (i.e. disabled after 1 instance has been added)?
  22. check if registered sidebar is active & has widget content
  23. How to edit widgets in WordPress
  24. dynamically add scripts to WP_Widget widget() method
  25. Extend WordPress 3.8 Site Activity Dashboard Widget to include more comments
  26. How to use is_active_widget?
  27. Get a list of all Widgets registered in WordPress admin widgets-area
  28. Modifying the default search widget
  29. Adding iframe Content to Sidebar Widget
  30. Enabling jquery when dragging available widget to sidebar area
  31. How Do I Add Custom CSS To Only Certain Widgets
  32. Get Widget Instance inside Widget
  33. Show post tags in a widget
  34. Author template, filter sidebar widgets by author?
  35. Prevent widgets removal
  36. Add the sidebar/widget editor to the post edit screen?
  37. WordPress 4.8: Using multiple WYSIWYG-Editors with media inside of Widgets how to?
  38. Does the number of widgets installed affect website performance?
  39. WP_Widget deprecated error in WordPress V4.3
  40. What is the quickest way to make a widget?
  41. How to display custom widget anywhere
  42. Show Woocommerce minicart widget in checkout page sidebar? And, how to make this update secure by overriding widget?
  43. Get page IDs based on which template they are using?
  44. Changes in widget customizer not triggering ‘save and publish’
  45. Add filter to blogroll widget
  46. Widget Options Not saving
  47. Indexes of widgets instances starts with ‘2’ in ‘wp_options’ table
  48. How to use wp_dequeue_style() for style enqueued in WP_Widget class
  49. Complex widget form UI – examples and best practices
  50. How do i manually place a widget code
  51. How to override the wordpress default widget markup
  52. Why is the $old_instance needed for the widget update function?
  53. Embed iframe or html page into dashboard widget
  54. Storing custom dashboard widget options in wordpress
  55. Get sidebar parameters (before_widget, before_title, etc.) from within a widget
  56. Prevent second widget activation
  57. How can I add links to widget titles?
  58. How to refresh Theme Customizer after change color inside wpColorPicker?
  59. How to add image uploader to a custom widget?
  60. PHP Coding Standards, Widgets and Sanitization
  61. Different widgets on different page templates?
  62. How to Remove All Widgets from Dashboard?
  63. Widgets not displaying in theme customizer
  64. $post>ID displays wrong post ID
  65. Register multiple sidebars
  66. Is there a way to add more tags to the tag cloud?
  67. Modify recent post sidebar to show post thumbs with out plugins
  68. Loop through widgets in sidebar
  69. How do I rebind event after widget save
  70. Programmatically edit the text of a widget
  71. How to add color picker to widgets?
  72. Where to find the source code of a widget?
  73. How to add author details in the post sidebar?
  74. How to delete cached transients from a widget instance properly?
  75. What is this instance variable doing in the Widgets class
  76. How to add a filter to all widget output
  77. Determining a Widget Instance and Sidebar Location?
  78. How to Validate Widget Input
  79. How to build widget with arrays inside arrays?
  80. Randomize widgets displayed in my sidebar [duplicate]
  81. How do I save data from submitted form from widget
  82. Calling static method in the Widget Class
  83. Video Embed in Sidebar Widget with Links to Others in Category
  84. Remove before_widget / after_widget content from Wp_Widget_Text
  85. How to add Custom Fields to Settings in Widget Options for all Registered Widgets?
  86. Why Can’t wp_editor Be Used in a Custom Widget?
  87. Shortcode from a widget is wrapped in unwanted element
  88. link wordpress and stackoverflow
  89. How to listen to color changes on the color picker?
  90. Remove All Widgets from Sidebar
  91. Woocommerce Product Category Widget – hide categories that have no products in stock [closed]
  92. Where to get the unsaved list of widgets in customizer?
  93. Insert a span inside widget title to give a different color to the second word
  94. Add New Footer Widget Area with Limited Options?
  95. Widget with random posts from a blog for external sites
  96. Widget Update Code Not Working
  97. Add div class to only one widget
  98. How to use control_callback when creating a widget via functions.php or plugin?
  99. Remove […] from RSS feed?
  100. Why will using __construct instead of widget_class_name when creating widget trigger out of memory error
Categories widgets Tags post-meta, widgets
Next 10 posts data of currently viewing post in blog page
pluggable function in theme, to be overridden by plugin

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress