0

I am working on a wordpress meta box that will have repeatable dropdown fields made up of a list of users from the site that is queried from the database. I am having trouble getting the array to output in the manner that is outlined on this github.

Here is my current code:

<?php
global $wpdb;
$users = $wpdb->get_col( "SELECT display_name FROM {$wpdb->prefix}users WHERE ID !=1 ORDER BY display_name ASC" );
foreach ( $users as $user ) {
echo '<option value="'.$user.'">'.$user.'</option>';
}
?>   

The current output looks like so:

<option value="User Name">User Name</option>

I need the output to look like this:

$options = array (
        'Option 1' => 'option1',
        'Option 2' => 'option2',
        'Option 3' => 'option3',
        'Option 4' => 'option4',
    );

    return $options;

The way that the dropdown is generated is as follows:

<select name="select[]">
<?php foreach ( $options as $label => $value ) : ?>
<option value="<?php echo $value; ?>"<?php selected( $field['select'], $value ); ?>><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
2
  • can you show what your users have after the query, and tell us what do you want to show in the select input? Commented Mar 17, 2017 at 9:52
  • see my update - it works for a typical dropdown now, but I cant get it to work with the repeatable fields jquery without changing the markup Commented Mar 17, 2017 at 9:56

1 Answer 1

1

Your query is wrong then, get_col only returns one row.

Instead of get_col use get_results .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.