0

In plugin development, how to fetch data from database of particular data in dropdown list?

global $wpdb;
$id = 'parent_id';
$data = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE meta_key = $id"); 
echo $data;

i get results as array how can i get all values in dropdown list? Can you please help?

0

The easiest way would be to iterate through your array with a for loop and create your own drop down.

0
            <select name="parent_id" id="parent-id">
                <?php 
                    $sql = "SELECT meta_value FROM wp_postmeta WHERE meta_key='parent_id'";
                    $results = $wpdb->get_results($sql) or die(mysql_error());    
                    foreach( $results as $result ) {    
                ?>
                <?php echo $result->meta_value;?>           
                    <option <?php if ($result->meta_value == $parent) echo 'selected="selected" ';?> >
                        <?php //echo $result->meta_value; ?>
                        <?php echo $result->meta_value; ?>
                    </option>
                <?php 
                    }
                ?>                      
            </select> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.