I dont know how to code and sorry for my bad english, so i found a code here to make a random views on my post views using $count = rand(700,999); which make a random views for my post but the problem is everytime a user click a post views changes again and not update the meta data.
function ktz_setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 1;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
$count = rand(700,999);
update_post_meta($postID, $count_key, $count);
}
return $count; /* so you can show it */
What i want is everytime a user click a post the views add up not changes to 700 - 999..
please help i want to make a fake views for my site..
I dont know how to code and sorry for my bad english, so i found a code here to make a random views on my post views using $count = rand(700,999); which make a random views for my post but the problem is everytime a user click a post views changes again and not update the meta data.
function ktz_setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 1;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
$count = rand(700,999);
update_post_meta($postID, $count_key, $count);
}
return $count; /* so you can show it */
What i want is everytime a user click a post the views add up not changes to 700 - 999..
please help i want to make a fake views for my site..
Share Improve this question edited Jun 19, 2019 at 1:30 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Jun 17, 2019 at 14:50 jonardjonard 11 bronze badge 4- you can manually update views in your database. Or you need to set this function in a while for all your posts, but not load it on each post display. – Gregory Commented Jun 17, 2019 at 14:57
- Thank you for the reply, is there no other way to make this work? if i put that rand(700,999); to views is not adding up instead it always generate 700 t 999 views each time users click that post.. – jonard Commented Jun 19, 2019 at 16:00
- each time you display your page, you ask to generate a new number, so, no, if you don't save this data only one time, it will never be the same. – Gregory Commented Jun 19, 2019 at 19:32
- another solution, set them all as empty, and if empty, fill it, if not, don't do anything. – Gregory Commented Jun 19, 2019 at 19:33
1 Answer
Reset to default 0Use something like this : (add it in your function.php) and load your site once. after it finish, deactivate the function. (don't forget to declare your $postID)
function my_update_posts() {
$count_key = 'post_views_count';
$args = array(
'post_type' => 'post',
'numberposts' => -1
);
$myposts = get_posts($args);
foreach ($myposts as $mypost){
$mypost->post_title = $mypost->post_title.'';
$count = rand(700,999);
update_post_meta($postID, $count_key, $count);
wp_update_post( $mypost );
}
}
add_action( 'wp_loaded', 'my_update_posts' );