Вывод количества просмотров
Здравствуйте. Есть код в функции для вывода количества просмотров статьи
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
Регистрирует переход на страницу в single.php (Добавляется туда)
<?php setPostViews(get_the_ID()); ?>
И сам вывод
<?php echo getPostViews(get_the_ID()); ?>
Далее этот вывод сую в цикл get_posts:
<div class="tabs-container active" id="tab1">
<div class="pst-block-main">
<div class="col-row">
<?php
$args = array(
"post_type" => "post",
"category" => "2",
"posts_per_page" => 5
);
$posts = get_posts($args);
?>
<?php if($posts) : ?>
<?php setup_postdata($posts[0]); ?>
<div class="col-half">
<article class="post post-tp-5 zoom">
<figure>
<a href="<?php echo get_permalink( $posts[0]->ID ); ?>">
<?php echo get_the_post_thumbnail( $posts[0]->ID, $size = 'homepage-567x393', array("class" => "adaptive") ); ?>
</a>
<div class="ptp-5-overlay">
<div class="ptp-5-data">
<a href="">
<i class="li_eye"></i><?php echo getPostViews(get_the_ID()); ?>
</a>
<a href="">
<i class="li_bubble"></i><?php comments_number('0', '1', '%'); ?>
</a>
</div>
</div>
</figure>
<h3 class="title-5"><a href="<?php echo get_permalink( $posts[0]->ID ); ?>"><?php echo $posts[0]->post_title; ?></a></h3>
<div class="meta-tp-2">
<div class="date"><span><?php echo get_the_date( "d.m.y", $posts[0]->ID ); ?></span>
</div>
</div>
<p class="p"><?php the_excerpt(); ?></p>
</article>
</div>
<?php wp_reset_postdata(); ?>
<div class="col-half">
<?php $i = 1; foreach($posts as $post) : ?>
<?php $lastposts[5] ?>
<?php if($i > 0) : ?>
<article class="post post-tp-3">
<figure class="picture">
<a href="<?php echo get_permalink( $post->ID ); ?>">
<?php echo get_the_post_thumbnail( $post->ID, $size = 'homepage-126x98', array("class" => "adaptive") ); ?>
</a>
</figure>
<h3 class="title-3"><a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo $post->post_title; ?></a></h3>
<div class="date-tp-2"><?php echo get_the_date( "d.m.y", $post->ID ); ?></div>
</article>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
Вопрос: Почему данные отличаются, например новость на главной выводится с этим циклом с количеством например 30, а по факту если перейти на саму запись будет 1000 и цифра эта правильная. И иногда неправильная цифра сама рандомно меняется. В чем мб проблема? Спасибо.
на
Спасибо)