I am making a widget and I have used the wp_dropdown_categories() function in that widget. What I can't figure out is how to get the selected value from the dropdown.

What variable is the selected stored in?

$args = array('show_option_none' => 'No Category','hide_empty' => 0);
wp_dropdown_categories( $args );
  • What do you want to do with the value? As in HTML the selected value is available as selected within the option – Sven Dec 21 '14 at 20:57
  • I want to make a category loop based on the chosen category – Jamie Dec 21 '14 at 21:50
  • 1
    Maybe you're better of using get_categories() to loop through an array of categories instead :) – Sven Dec 21 '14 at 21:58
  • 2
    Your 100th question :) – kaiser Dec 21 '14 at 22:55
  • can you post a code snippet to show what you mean Sven ? I have not been able to get this working – Jamie Dec 22 '14 at 2:09
up vote 6 down vote accepted

We can get the categories via get_categories() function (which will get the same categories as wp_dropdown_categories() function), but as array and without the markup.

As the value is returned as array, we can loop through the categories and generate the HTML ourself.

Usually, we would aim for a structure like this:

<select name="categories">
  <option value="1">Category 1</option>
  <option value="2">Category 2</option>
  <option value="3">Category 3</option>
</select>

So we're going to generate the structure from above via PHP by looping through the $categories array:

echo '<select name="categories">';

  // Get categories as array
  $categories = get_categories( $args );
  foreach ( $categories as $category ) :

    echo '<option value="' . $category->term_id . '">' . $category->name . '</option>';

  endforeach;

echo '</select>';

Feel free to modify the function via the $args parameters (see Codex for reference).

Now we'll just need to add the selected attribute within the <option>, so we're going to add this via a short if statement to compare the current $category->term_id and the one saved in the database $stored_category_id:

$stored_category_id = 10; // This is the value from the database
$selected = ( $stored_category_id == $category->term_id  ) ? 'selected' : '';

And that's it! Your complete snippet (maybe with a default option at the beginning) should now look something like this:

echo '<select name="categories">';
  // Add custom option as default
  echo '<option>' . __('No Category', 'text-domain') . '</option>';

  // Get categories as array
  $categories = get_categories( $args );
  foreach ( $categories as $category ) :

    // Check if current term ID is equal to term ID stored in database
    $selected = ( $stored_category_id ==  $category->term_id  ) ? 'selected' : '';

    echo '<option value="' . $category->term_id . '" ' . $selected . '>' . $category->name . '</option>';

  endforeach;

echo '</select>';

Good luck! :)

  • 1
    Thanks Sven, My main issue what I wasn't sure how to write up the select in php while getting the values from the database. What really surprises me is the lack of information in the codex. – Jamie Dec 23 '14 at 0:13

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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