Gist 67badef3655577f9aff44e57c0e7d8c0
✓ Published0🌍 Public
KKopylovVitalik
Last edited Jun 3, 2020
Created on Jun 3, 2020
This WordPress PHP snippet implements a post view counter by storing counts in post metadata. Two functions handle the logic: `getPostViews()` retrieves the current count and returns a formatted string, while `setPostViews()` increments the count and updates the stored value via the WordPress metadata API functions `get_post_meta`, `add_post_meta`, `update_post_meta`, and `delete_post_meta`. The code also removes the default `adjacent_posts_rel_link_wp_head` action to prevent prefetching from artificially inflating the view count. The example demonstrates a simple server-side approach to tracking content engagement without external analytics libraries.
AI-generated description