PHP - Create Shortcode to Count Post Views in WordPress
Step 1:
Create a PODS custom field for your POST / PAGE
Create a NUMBER field
Name it page_view (lowercase, no capitalization)
Step 2:
Find your functions.php file in your theme > wp-content \ themes \ your-theme \ functions.php
Step 3:
Use the PHP code below
<pre data-lang="PHP"><code>function create_view_counter(){
/page_view will be the name of field created in PODS/
$view = get_post_meta(get_the_ID(), 'page_view', true);
/ check if your field is empty /
if ($view == ''){
$view = 0;
}
/auto add 1 view to your counter /
$view = $view+1;
/update your counter back to WP database /
update_post_meta(get_the_ID(), 'page_view', $view);
/display the counter in your page /
return "View: $view";
}
//create the shortcode by the function above
add_shortcode ('viewcounter', 'create_view_counter');</code></pre>
Step 4:
Place the shortcode in your post content (if you don't add it, it won't count), so it's best to place it where you want views to be counted—for example, in your theme or footer...
[viewcounter]