2017-01-07 1 views
0

내 개인 WordPress 테마에 대한이 코드를 사용하여 한 개의 사용자 정의 페이지에 인기있는 게시물을 표시하려고합니다. 불행히도 3 개의 모든 UL 블록에 동일한 게시물을 표시합니다. 아이디어는 3 일 동안 가장 많이 본 게시물을 표시 한 다음 다른 블록에서 7 일 동안 가장 많이 본 게시물을 표시하고 마지막으로 30 일 이내에 가장 많이 본 블로그를 표시합니다. 나는 지금 왜 일하지 않는가.조회수, 3 일, 7 일, 30 일간 인기 게시물

// Top post by views in last 3 day's 
<ul> 
    <?php 
    function filter_where($where = '') { 
     $where .= " AND post_date > '" . date('Y-m-d', strtotime('-3 days')) . "'"; 
     return $where; 
    } 
    add_filter('posts_where', 'filter_where'); 
    query_posts('post_type=post&posts_per_page=5&orderby=post_views_count&order=DESC'); 
    while (have_posts()): the_post(); ?> 
     <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> 
     <?php 
    endwhile; 
    wp_reset_query(); 
    ?> 
</ul> 

// Top post by views in last 7 day's 
<ul> 
    <?php 
    function filter_where2($where = '') { 
     $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'"; 
     return $where; 
    } 
    add_filter('posts_where', 'filter_where2'); 
    query_posts('post_type=post&posts_per_page=5&orderby=post_views_count&order=DESC'); 
    while (have_posts()): the_post(); ?> 
     <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> 
     <?php 
    endwhile; 
    wp_reset_query(); 
    ?> 
</ul> 

// Top post by views in last 30 day's 
<ul> 
    <?php 
    function filter_where3($where = '') { 
     $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'"; 
     return $where; 
    } 
    add_filter('posts_where', 'filter_where3'); 
    query_posts('post_type=post&posts_per_page=5&orderby=post_views_count&order=DESC'); 
    while (have_posts()): the_post(); ?> 
     <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> 
     <?php 
    endwhile; 
    wp_reset_query(); 
    ?> 
</ul> 

답변

0

WP_Query은 (query_posts이 클래스를 사용)로도 orderby 값이 인식되지 않는다.

게시물이로드 될 때마다 사용자 정의 필드 post_view_count을 추가 (및 증분)하지 않으면 언급하지 않았습니다. 이 사건이 있다면

, 당신은 당신의 query_posts을 변경할 수 있습니다

add_filter('posts_where', 'filter_where'); 
$args = array('post_type'=>'post', 
      'posts_per_page'=>5, 
      'orderby'=>'meta_value_num', 
      'order'=>'DESC', 
      'meta_key'=>'post_views_count' 
); 
$query = new WP_Query($args); 

while ($query->have_posts()) { 
    $query->the_post(); ?> 
    <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> 
    <?php 
} 
wp_reset_query(); 

왼쪽 2 조회에이 논리를 계속합니다.

당신은 도움이 희망 WP_Query reference

에 대한 모든 세부 사항을 찾을 수 있습니다.

+0

이것은 작동하지 않습니다. 하지만 고마워. – bulldozer

+0

게시물 조회수를 계산하는 메타가 있습니까? 새로운 WP_Query로 query_posts를 변경 했습니까? – Benoti

+0

불행히도, 모르겠다. WP의 새로운 기능. – bulldozer