2015-03-18 112 views
0

我想在wordpress中的自定义查询中返回帖子的等级。我的代码目前是Wordpress在查询中查找帖子索引

function post_rank($post_type,$meta_value,$post_id) { 

$args = array (

    'post_type'    => $post_type, 

    'meta_key'  => 'totalvotes', 

    'order'     => 'DESC', 

    'orderby'    => 'meta_value_num', 

    'post_status'    => '\'submitted\' 

); 

$the_query = new WP_Query($args); 

$total_in_search = $the_query->found_posts; 



$rank_in_search = array_search ($the_query,$post_id) +1 ; 

$overall= $rank_in_search ."/".$total_in_search ; 

wp_reset_postdata(); 

return $overall; 

} 

但我似乎无法找到帖子索引。任何想法,将不胜感激。

回答

0

据商务部http://codex.wordpress.org/Class_Reference/WP_Query它必须是

$current_post (available during The Loop) Index of the post currently being displayed. 
+0

你知道在循环之外是否有办法做到这一点? – VC10 2015-03-19 12:24:00

+0

关于这篇文章有什么信息吗? – yuyokk 2015-03-19 12:28:17

+0

post_id。我想要做的是获得我的帖子内的职位排名元总票数排序。 – VC10 2015-03-19 21:04:43

0

我最终使用的循环如下:

while ($the_query->have_posts()) : $the_query->the_post(); 

      if ($post_id == get_the_ID()) 
      { 
       $rank_in_search = $i;    } 

$i++; 

     endwhile; 

不干净的做法,但暂时会做。任何改进总是赞赏