在使用GET_RESULTS后,我应该在哪里重置查询?

时间:2017-12-06 作者:LTech

我有以下代码来填充重力表单中的复选框字段。我有一个field\\u id数组,通过foreach循环循环。$arr = array(5, 6, 7, 8, 9);然后,我有了第二个foreach循环,其中包含一个从数据库中提取数据的查询。

但是,代码正在运行,每个后续的field\\u id都显示该field\\u id和之前的field\\u id的结果。例如field\\u id=6显示field\\u id=5和field\\u id=6的复选框字段列表。field\\u id=7显示field\\u id=5、field\\u id=6和field\\u id=7等的复选框字段列表。

function populate_checkboxes( $form ){

foreach( $form[\'fields\'] as &$field )  {
    $arr = array(5, 6, 7, 8, 9);

    foreach($arr as $field_id){
      global $wpdb;
      $query = $wpdb->get_results( "SELECT sefer FROM bulk_import where field_id_select_items = {$field_id}" );
      foreach($query as $sefer){

        if ( $field->id != $field_id ) {continue;}
        $choices[] = array( \'text\' => \'Select All\', \'value\' => \'Select_all\' ); 
        $inputs[] = array( \'label\' => \'Select All\', \'id\' => \'1\'); 
        //get all $sefer values
        $posts = get_posts(array(
        \'post_type\'=> array(\'learning_item\'),   \'suppress_filters\' => 0, \'posts_per_page\' => -1, \'order\' => \'asc\', 
        \'meta_query\' => array(  
            array( \'key\' => \'taken\',   \'value\' => \'0\'),
            array(\'key\' => \'torah\', \'value\' => $sefer -> sefer)
        ),
        ));

        $input_id = 2;
        foreach( $posts as $post ) {
            //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
            if ( $input_id % 10 == 0 ) {$input_id++;}
            $choices[] = array( \'text\' => $post->post_title, \'value\' => $post->post_title );
            $inputs[] = array( \'label\' => $post->post_title, \'id\' => "{$field_id}.{$input_id}" );

            $input_id++;
        }

        $field->choices = $choices; 
        $field->inputs = $inputs; 

      }
        //reset query here? 
         wp_reset_query();
    }

}
return $form;}
是否需要在某个位置重置查询?我尝试添加wp\\u reset\\u query();在几个不同的地方,但都不起作用。我需要在哪里重置查询?

1 个回复
SO网友:LTech

添加以下内容修复了该问题

 foreach($query as $sefer){
                $choices = array();
                $inputs = array();

结束

相关推荐

PRE_GET_POST完全删除TAX_QUERY

我正在使用pre_get_posts 使用筛选$wp_query->set( \'tax_query\', $tax_query );I\'m probably better-off doing this with a new WP_Query, but if someone knows something I don\'t I\'d love to hear it问题,我需要设置默认分类法;保留显示所有帖子的功能。例如:/cpt/?taxo=this 显示所有具有taxo术语==此的cpt。$ta