Query is not work

时间:2012-08-24 作者:Ronin

我在函数中使用此代码。php跟踪帖子视图。

/* Functions to track Post Views*/
 function getPostViews($postID){
 $count_key = \'post_views\';
 $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 View";
}
return $count.\' view\';
 }
 function setPostViews($postID) {
 $count_key = \'post_views\';
 $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);
 }
 }

 /* Track the Views for the post using wp_head hook */
 function trackPostViews ($post_id) {
 if ( !is_single() ) return;
 if ( empty ( $post_id) ) {
    global $post;
    $post_id = $post->ID;
 }
 setPostViews($post_id);
 }
 add_action( \'wp_head\', \'trackPostViews\');
但我想在侧边栏上显示我浏览次数最多的帖子,这是行不通的。有显示的帖子,但不是浏览最多的。这是我的问题。

             <?php

 function filter_where2($where = \'\') {
 $where .= " AND post_date > \'" . date(\'Y-m-d\', strtotime(\'-10 days\')) . "\'";
 return $where;
 }
 if ( ! $my_paged = absint( get_query_var( \'paged\' ) ) )
 $my_paged = 1;

 if ( ! $my_query = get_transient( "7677recent_$my_paged" ) ) 

 {
add_filter(\'posts_where\', \'filter_where2\');
$my_query = new WP_Query( "meta_key=post_views&orderby=meta_value&order=DESC&posts_per_page=5&paged=$my_paged" );
set_transient( "7677recent_$my_paged", $my_query, 60 * 60 );
 }
 remove_filter( \'posts_where\', \'filter_where2\' );
 while ($my_query->have_posts()) : $my_query->the_post(); 
 ?>

2 个回复
最合适的回答,由SO网友:Ronin 整理而成

我发现一个问题:)

$my\\u query=新建WP\\u查询(“meta\\u键=post\\u视图和排序依据=meta_value_num&;订单=描述(&D);posts\\u per\\u page=5(&;paged=$my\\u paged”);

SO网友:Damien

我想问题出在你的短暂

set_transient( "7677recent_$my_paged", $my_query, 60 * 60 );

目前每1小时刷新一次,您可以尝试将睾丸的刷新时间改为3或5分钟

结束