Support » Plugins » Hacks » select list of CUSTOM TAXONOMY with the option multiple

  • Resolved mcfmullen


    I’ve been wondering forever how to get a multi-select dropdown of my categories and finally found my answer here in the form of:

    <?php $select_cats = wp_dropdown_categories( array( 'echo' => 0 ) );
    $select_cats = str_replace( "name='cat' id=", "name='cat[]' multiple='multiple' id=", $select_cats );
    echo $select_cats; ?>

    Which is AMAZING. However, I need it to go one step further and have a multi-select dropdown of a custom taxonomy called regions.

    How can I accomplish this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz


    Just specify your own taxonomy as one of the arguments in the passed array. It defaults to category, but can be overridden.

    array( 'echo' => 0,
      'taxonomy' => 'regions' )

    Depending on the taxonomy, other arguments may be needed, like if it’s hierarchical. You can add any arguments that would be accepted by get_terms(), which is how wp_dropdown_categories() retrieves data from the database.

    Glad to know it is that simple!

    I now discovered a need to take this one step further. I required the name of the input field to be in this format:

    name="scrape[<?php echo $inpCnt; ?>][sponsors]"

    Given that my code looks like this:

    $sponsors = wp_dropdown_categories( array(
    		'child_of' => 0,
    		'class' => 'postform',
    		'depth' => 0,
    		'echo' => 0,
    		'exclude' => '',
    		'hide_empty' => false,
    		'hide_if_empty' => false,
    		'hierarchical' => true,
    		'order' => 'ASC',
    		'orderby' => 'name',
    		'selected' => 0,
    		'show_count' => 0,
    		'show_option_all' => '',
    		'show_option_none' => __(''),
    		'tab_index' => 0,
    		'taxonomy' => 'sponsors', )
    	$sponsors = str_replace(
    		"name='cat' id=",
    		"name='cat[]' multiple='multiple' size='19' id=",

    How do I do it?

    Moderator bcworkz


    Do you mean the <select> tag for drop down lists? You would add
    'name' => "scrape[$inpCnt][sponsors]",
    to the array definition. But if scrape is a javascript array, this will not work unless the whole thing is being echoed into a script block.

    it is a php array.

    Moderator bcworkz


    Not PHP without a $ in front, nor inside quotes. If the ‘name’ element is to be assigned a value in the array $scrape it should look like this:
    'name' => $scrape[$inpCnt]['sponsors'],

    you were correct the first time, thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘select list of CUSTOM TAXONOMY with the option multiple’ is closed to new replies.