List Custom Post Type Taxonomies, then the terms

353 Views Asked by At

I am trying to create an upload form so users can upload posts, but in it, I need them to be able to select a Taxonomy and then the Terms from the selected taxonomy.

I have two custom post types, 'Lost' and 'Found'. Inside both, there are about 15 taxonomies. Each taxonomy has between 5 - 10 terms. The user should be able to select a taxonomy and then the term is created in a second select box.

At the moment, I am having problems even display the taxonomies for each CPT. I have used the following to show all the custom taxonomies available, but I need to show them for just one CPT.

<form>
    <select>

        <?php 
        $args = array(
            'public'   => true,
            '_builtin' => false,

            ); 
                $output = 'names'; // or objects
                $operator = 'and'; // 'and' or 'or'
                $taxonomies = get_taxonomies( $args, $output, $operator ); 
                    if ( $taxonomies ) {
                        foreach ( $taxonomies  as $taxonomy ) {
                        echo '<option>' . $taxonomy . '</option>';
                    }
                }

        ?>
    </select>
</form>

I then tried targeting just one CPT, however, nothing displays then at all in the select box:

<form>
    <select>

        <?php 
       if(is_singular('lost')){
        $args = array(
            'public'   => true,
            '_builtin' => false,

            ); 
                $output = 'names'; // or objects
                $operator = 'and'; // 'and' or 'or'
                $taxonomies = get_taxonomies( $args, $output, $operator ); 
                    if ( $taxonomies ) {
                        foreach ( $taxonomies  as $taxonomy ) {
                        echo '<option>' . $taxonomy . '</option>';
                    }
                }
            }
        ?>
    </select>
</form>

So, how can I get a list of all taxonomies for a specific Custom Post Type and then, fill a second select box with the terms of the selected Taxonomy?

I hope someone can help

Thanks!

0

There are 0 best solutions below