Ver Mensaje Individual
  #6 (permalink)  
Antiguo 25/07/2013, 11:12
designermaster
 
Fecha de Ingreso: septiembre-2008
Ubicación: España
Mensajes: 230
Antigüedad: 15 años, 7 meses
Puntos: 0
Respuesta: Posts más populares en base a las visitas

Mejor, pongo el código que estoy usando y así vamos al kit del asunto:


- - - - -- - -

CATEGORY.PHP

<?php
$popularpost = new WP_Query( array( 'posts_per_page' => 5, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();

?>


<a href="<?php the_permalink(); ?>">
<li>
<div class="info"><span>Featured photo</span><h1><?php the_title(); ?></h1></div>
<div class="mascara"></div>
<div class="imagen"><?php the_post_thumbnail( 'category-thumb' ); ?></div>
</li>
</a>

<?php

endwhile;
?>



- - - - - - - -

FUNCTION.PHP
Este código guarda las visitas de cada post:




function wpb_set_post_views($postID) {
$count_key = 'wpb_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);
}
}
//To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);


function wpb_track_post_views ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
wpb_set_post_views($post_id);
}
add_action( 'wp_head', 'wpb_track_post_views');