Popular post

Popular post using post view

This code using to display popular post. set those code functions.php file.

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);
}
}

After set code then next step call this function single.php file.

<?php
setPostViews(get_the_ID());
?>

And Last step is custom query for.

<?php $args = array(
‘posts_per_page’   => $post_page,
‘meta_key’            => ‘post_views_count’,
‘orderby’           => ‘meta_value_num’,
‘order’            => ‘DESC’,
‘post_type’        => ‘post’,
‘post_status’      => ‘publish’,
‘paged’            => $paged
);

$single_post = new WP_Query( $args );
while ( $single_post->have_posts() ) {
$single_post->the_post();
}
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s