Recreating the hierarchy of taxonomies for a dropdown form menu?

I have a form that contains a dropdown menu. This form is on a page form.php and is not part of my theme. It exists outside of my site. This dropdown menu contains my list of values for a custom taxonomy called Formats. My Formats are as follows:

Entry Form
Facebook
  - Entry Form
  - Page
Twitter

This is my code so far:

Read More
<?php include("wp-blog-header.php"); ?>
<?php 
global $wpdb;
$getFormats = $wpdb->get_results($wpdb->prepare("

        SELECT * FROM wp_terms p 

        LEFT OUTER JOIN wp_term_taxonomy t ON p.term_id = t.term_id

        WHERE t.taxonomy = 'format'

        ")); ?> // This gets all the values for the format taxonomy

<form> //start form
<select name="format"> //start dropdown

<?php
foreach ($getFormats as $format) { //spit out the formats
    echo "<option value='".$format->name."'>".$format->name."</option>";
} ?>

</select> //end dropdown
</form> // end form
?>

So far, my dropdown looks like this:

Entry Form
Facebook
Entry Form
Page
Twitter

First question: How can I get the hierarchy to stick (i.e. keep the indents)?

My HTML output is as follows:

<option value="Entry Form">Entry Form</option>
<option value="Facebook">Facebook</option>
<option value="Entry Form">Entry Form</option>  // <---- this is gonna be a problem
<option value="Page">Page</option>
<option value="Twitter">Twitter</option>

Second Question: You’ll notice the output for the second instance of Entry Form is identical to the first instance. Obviously, this is going to be a problem for submitting the form. How can I make this unique?

Related posts

Leave a Reply

1 comment

  1. You can use the standard WordPress function, get the dropdown already formatted and solve both problems at once.

    Like so:

    define( 'WP_USE_THEMES', false );
    require( './wp-load.php' );
    
    wp_dropdown_categories(
        array(
            'child_of' => 0,
            'class' => 'postform', 
            'depth' => 0,
            'echo' => 1,
            'exclude' => '', 
            'hide_empty' => false, 
            'hide_if_empty' => false,
            'hierarchical' => true,
            'id' => '',
            'name' => 'cat-dropdown', 
            'order' => 'ASC',
            'orderby' => 'name', 
            'selected' => 0, 
            'show_count' => 0,
            'show_option_all' => '', 
            'show_option_none' => __('None'),
            'tab_index' => 0, 
            'taxonomy' => 'category',
        )
    );
    

    Outputs:

    <select name='cat-dropdown' id='cat-dropdown' class='postform' >
        <option value='-1'>None</option>
        <option class="level-0" value="2">Other</option>
        <option class="level-0" value="1">Uncategorized</option>
        <option class="level-1" value="4">&nbsp;&nbsp;&nbsp;Other</option>
        <option class="level-2" value="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Other</option>
    </select>
    

    For the default category:
    categories list