Skip to content

Instantly share code, notes, and snippets.

@PeterRoberto
Created September 18, 2017 12:23
Show Gist options
  • Save PeterRoberto/c6e0ebb855e683ceedf8c186d21a6e40 to your computer and use it in GitHub Desktop.
Save PeterRoberto/c6e0ebb855e683ceedf8c186d21a6e40 to your computer and use it in GitHub Desktop.
Listas posts mais vistos/lidos - WORDPRESS
// POSTS MAIS VISTOS (NO FUNCTIONS)
function shapeSpace_popular_posts($post_id) {
$count_key = 'popular_posts';
$count = get_post_meta($post_id, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
} else {
$count++;
update_post_meta($post_id, $count_key, $count);
}
}
function shapeSpace_track_posts($post_id) {
if (!is_single()) return;
if (empty($post_id)) {
global $post;
$post_id = $post->ID;
}
shapeSpace_popular_posts($post_id);
}
add_action('wp_head', 'shapeSpace_track_posts');
<!-- LISTAGEM DE POSTS MAIS LIDOS -->
<ul>
<?php $popular = new WP_Query(array( 'posts_per_page'=>3, 'meta_key'=>'popular_posts', 'orderby'=>'meta_value_num', 'order'=>'DESC'));
while ($popular->have_posts()) : $popular->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_query(); wp_reset_postdata(); ?>
</ul>
<!-- ## -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment