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

Wp Super Cache stops the update of a Post Meta

When using WP Super Cache, the plugin generates static HTML files for your templates, which means that any PHP code you’re running will only ever run when the cached page is generated or regenerated.

So if you need to trigger PHP code on every page load from behind a cache, you’ll need to use AJAX.

So for your use case you’ll need two functions added to your functions file. Firstly you’ll need to add the Javascript that makes the AJAX request to the footer of all of your posts. This will do that:

function my_count_views_script() {
    if ( is_single() ) :
        ?>
        <script>
            jQuery.post({
                url:     '<?php echo admin_url( 'admin-ajax.php' ); ?>',
                action:  'my_count_views',
                post_id: <?php the_ID(); ?>
            });
        </script>
        <?php
    endif;
}
add_action( 'wp_footer', 'my_count_views_script' );

This sends a request to the admin-ajax.php file with the post_id of the post being viewed and the action we want to perform with it.

To count this view, you need to tell admin-ajax.php what code to run when it received a request with the my_count_views action. You do this by hooking onto wp_ajax_my_count_views (to handle it for logged in users) and wp_ajax_nopriv_my_count_views for logged out users:

function my_count_views() {
    if ( isset( $_POST['post_id'] ) && $_POST['post_id'] ) {
        $post_id = intval( $_POST['post_id'] );
        $views = intval( get_post_meta( $post_id, 'postsViews', true ) ) ?: 0;
        $views += 1;

        update_post_meta( $post_id, 'postsViews', $views );
    }

    wp_die();
}
add_action( 'wp_ajax_my_count_views', 'my_count_views' );
add_action( 'wp_ajax_nopriv_my_count_views', 'my_count_views' );

Related Posts:

  1. How to sanitize select box values in post meta?
  2. Strategy to get post meta for use outside the loop
  3. Set Expiration Date of a Post from the Frontend with wp_insert_post
  4. Is there a need for nonce with Post Metabox?
  5. Metadata Query when storing data as array possible?
  6. How to show specific post meta?
  7. Running a function on post update with new post meta
  8. Is it possible to set a variable for get_post_meta?
  9. Get attachment meta (I need attachment ID, unsure how to find it)
  10. How can I sort my loop based on meta data, using a form?
  11. Related Post Category Filter
  12. Make a custom field in admin post.php read only
  13. Change default options on attachment page
  14. After a while attachment meta data is missing “sizes” data
  15. Why is my Toggle field not showing in Gutenberg?
  16. How do I access the current post object within a block theme template or pattern?
  17. Menu items description? Custom Walker for wp_nav_menu()
  18. How to define and link full path to css located at a random folder on header.php
  19. Sizing screenshot.png without losing aspect ratio
  20. Software for WordPress Theme and Plugin Development? [closed]
  21. When to use is_home() vs is_front_page()?
  22. What is the preferred way to add custom javascript files to the site?
  23. Get the blog page URL set in Options
  24. How to create .pot files with POedit?
  25. How do I get the theme URL in PHP?
  26. Why when I submit a form in wordpress it loads a 404 page though URL is correct
  27. What is the constant WP_USE_THEMES for?
  28. Worthwhile to restrict direct access of theme files?
  29. How Do I Protect My Premium WordPress App Theme from Copying?
  30. How to detect mobile devices and present them a specific theme?
  31. Using wp_add_inline_style without a stylesheet
  32. How to add posts to custom menus?
  33. WordPress Theme Preview Image
  34. How To extend WP_Customize_Control
  35. What is the difference between front-page.php and home.php? [duplicate]
  36. How can I get page slug
  37. Does the functions.php file ever get called during an AJAX call? Debug AJAX
  38. When to use _e and __ for the translation?
  39. What is a Theme textdomain?
  40. How to show a custom meta box on the “Quick Edit” screen?
  41. How to add CSS class to custom logo?
  42. How to add custom css file in theme?
  43. How important is it to enqueue a theme’s stylesheet?
  44. CSS not updating in browser when I change it
  45. Display Search Result Count
  46. wp_nav_menu(), how to change class?
  47. theme path in javascript file
  48. Is wp_is_mobile() effective?
  49. Display Menu Name using wp_nav_menu
  50. Making update notification functionality for my themes
  51. What questions do you ask when asked to give an estimate on theme development?
  52. How to remove dashicons.min.css from frontend?
  53. How do I remove a pre-existing customizer setting?
  54. Minimum Template Files for Theme Development
  55. What’s the difference between home.php and index.php?
  56. pass object/JSON to wp_localize_script
  57. get_template_part vs action hooks in themes
  58. Theme Activate Hook
  59. Solutions for generating dynamic javascript / CSS
  60. wp_redirect() – headers already sent
  61. Can I create my own “Recent Posts” widget or customize the existing one?
  62. Custom single template for a specific category
  63. how can I add an icon/image for a child theme?
  64. How can I add a URL field to the attachments window?
  65. The the_post_thumbnail without srcset?
  66. The proper way to include/require PHP files in WordPress
  67. Get the first image from post content (eg.: hotlinked images)
  68. after_setup_theme always runs
  69. When to use add_action(‘init’) vs add_action(‘wp_enqueue_scripts’)
  70. Getting failure when using filemtime() with wp_enqueue_style
  71. Do I actually need to link my theme’s style.css in the theme files
  72. Why is wp_head() creating a top margin at the top of my theme header?
  73. How to store widget fields data as an array?
  74. How do I add version control to my workflow?
  75. Using classes instead of global functions in functions.php
  76. WordPress API Menu/Submenu Order
  77. How can I display a menu on certain pages only?
  78. Enqueue a stylesheet for login page and make it appear in head element
  79. Adding a div to wrap widget content after the widget title
  80. How to insert a logo in the header?
  81. How to set permalink structure via functions.php
  82. What is $post_id? is it a global variable in WordPress?
  83. How to override pluggable function in theme?
  84. How to move page template files like page-{slug}.php to a sub-directory?
  85. WooCommerce shop page to use my custom template [closed]
  86. Theme customizer – settings order
  87. What is the difference between wp_register_sidebar_widget and register_widget?
  88. Should we use get_template_part() in functions files instead of include_once?
  89. Reset positions of metaboxes in admin
  90. CSS classes for theme
  91. “Quick Edit” > update clears out my custom meta values
  92. How to enqueue script if widget is displayed on page?
  93. Custom Image section in Customizer
  94. A way to automatically install pages on theme install?
  95. Get entered value of customiser field for live preview
  96. Add custom classes to anchor in wp_nav_menu
  97. How to remove search bar from a wordpress theme? [closed]
  98. Upgrade from 5.0.4 to 5.1.1 causes $theme to be null
  99. Bestway To Define Theme and Plugin path and url
  100. Is there a has_more_tag() method or equivalent?
Categories theme-development Tags plugin-wp-supercache, post-meta, theme-development
How to recover permanently deleted blog posts?
Create default pages in wordpress multisite

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