Php 列举Wordpress中的帖子;我不能工作两次

Php 列举Wordpress中的帖子;我不能工作两次,php,wordpress,side-effects,Php,Wordpress,Side Effects,此方法是由某个人在Wordpress论坛上发布的,以从其ID(不按顺序排列)获取帖子“编号”(1、2、3、4等)。我对它做了一些修改,只查询幻灯片 function Get_Post_Number($postID){ $temp_query = $wp_query; $postNumberQuery = new WP_Query(array( 'ignore_sticky_posts' => 1, 'posts_per_page' =>

此方法是由某个人在Wordpress论坛上发布的,以从其ID(不按顺序排列)获取帖子“编号”(1、2、3、4等)。我对它做了一些修改,只查询幻灯片

function Get_Post_Number($postID){
    $temp_query = $wp_query;
    $postNumberQuery = new WP_Query(array(
        'ignore_sticky_posts' => 1,
        'posts_per_page' => 20,
        'post_type' => 'slide'));

    $counter = 0;
    $postCount = 0;
    if($postNumberQuery->have_posts()) :
        while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post();
            if ($postID == get_the_ID()){
                $postCount = $counter;
            } else {
                $counter++;
            }
    endwhile; endif;
    wp_reset_query();
    $wp_query = $temp_query;
    return $postCount;
}
只要调用一次,它就可以正常工作,并将返回正确的号码

Get_Post_Number(get_the_ID()) //Returns 3 (for instance)
但是,当调用两次时,它不再工作:

Get_Post_Number(get_the_ID()) //Returns 3 (for instance)
Get_Post_Number(get_the_ID()) //Returns 0 (wrong!)
这让我相信这个函数有副作用,但我不知道如何撤销它们。Wordpress说,在您完成自己的“自定义循环”后调用
wp\u reset\u query()
,但函数已经完成了这项工作


我该怎么办?

我刚刚删除了
wp\u reset\u query()
,它成功了。(即使Wordpress告诉您调用它!?)

它在多次调用时总是返回0,还是其他数字也返回0?@anthonygore仅返回0。
$postID==获取ID()
如果这是真的,$counter变量将始终为0。回显两个变量以进行测试,但我会说这就是发生的情况