Wordpress在查询中查找文章索引


Wordpress Find Index of Post in Query

我正试图在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;
}

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

根据单据http://codex.wordpress.org/Class_Reference/WP_Query它必须是

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

我最终使用了如下循环:

while ( $the_query->have_posts() ) : $the_query->the_post();
            if ($post_id == get_the_ID()) 
            {
                $rank_in_search = $i;             }
$i++;
        endwhile;

这不是最干净的方法,但暂时可以。任何改进都会受到的赞赏