I get a empty result in select2 with php “select(sql)”

Trios

New Member
#1
I want to search with select 2 in page wordpress and i want to get in result only private page, I get a number of li correct "14 li" and this what i have in my db but li foreach is empty why?
Mã:
$post_id = $wpdb->get_results("SELECT `ID`,`post_title` FROM $wpdb->posts WHERE `post_type` = 'page' and `post_status` = 'private'");

?>
<div class="form-group">
        <label class="col-md-3 control-label" for="service"><?php echo $lang['department']; ?> *</label>
        <div class="col-md-8">
          <select name="services[]" id="service" class="form-control select2-multiple" multiple>
              <?php if ( !empty( $post_id ) ) {
                        foreach ( $post_id as $r ) {             
                ?>
              <option value="<?php echo $post_id['ID']?>"><?php $post_id['post_title']?></option>
             <?php
                    }
                    }               
             ?>
          </select>
        </div>
    </div>

    <script>
            jQuery(document).ready(function() {
                    jQuery('#service').select2();
            });
    </script>
 

Admin

Administrator
Thành viên BQT
#2
$wpdb->get_results returns object by default plus in foreach loop you should get values from $r variable not $post_id. It should be $r->ID, $r->post_title
 
OP
OP
T

Trios

New Member
#3
i do that but with wrong method , $r['ID'] but now i use $r->ID and this solved my problem , thank you :D
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top