-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-unique-views.php
47 lines (41 loc) · 1.47 KB
/
post-unique-views.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
// Function to count post views by session
function count_post_views() {
if (is_single()) {
global $post;
$post_id = $post->ID;
// Check if the post ID is stored in the session
$viewed_posts = isset($_SESSION['viewed_posts']) ? $_SESSION['viewed_posts'] : array();
// If the post ID is not in the session, count the view and store the post ID in the session
if (!in_array($post_id, $viewed_posts)) {
$views = get_post_meta($post_id, 'post_views', true);
$views = $views ? $views : 0;
$views++;
update_post_meta($post_id, 'post_views', $views);
// Store the post ID in the session
$_SESSION['viewed_posts'][] = $post_id;
}
}
}
// Initialize session
function start_session() {
if (!session_id()) {
session_start();
}
}
// Create post views meta if not exists
function create_post_views_meta() {
global $wpdb;
$query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'post_views'";
$post_ids = $wpdb->get_col($query);
if (empty($post_ids)) {
$posts = get_posts(array('post_type' => 'post', 'numberposts' => -1));
foreach ($posts as $post) {
add_post_meta($post->ID, 'post_views', 0, true);
}
}
}
// Hook into WordPress actions
add_action('init', 'start_session');
add_action('init', 'create_post_views_meta');
add_action('wp_head', 'count_post_views');