6

I’ve created a custom post type (called Sponsors) with an additional taxonomy connected to it, called “Type”.

Everything is essentially working perfectly but I would very much like to have my taxonomy “Type” to be a dropdown menu instead of the usual “type and search or create a new”. The reason is, that I’ve created six different “Types” in the taxonomy and the people which are going to create new “sponsors” should only need to select one of those six for each new Sponsor post.

I’ve tried some tutorials and some guides found by Google but none which has yet to work.

The custom post type and the custom taxonomy is created by the plugin “Custom Post Type UI”.

What I tried so far is this:

add_action('restrict_manage_posts','my_restrict_manage_posts');

        function my_restrict_manage_posts() {
            global $typenow;

            if ($typenow=='sponsors'){
                         $args = array(
                             'show_option_all' => "Show All Categories",
                             'taxonomy'        => 'type',
                             'name'               => 'type'

                         );
                wp_dropdown_categories($args);
                        }
        }
add_action( 'request', 'my_request' );
function my_request($request) {
    if (is_admin() && $GLOBALS['PHP_SELF'] == '/wp-admin/edit.php' && isset($request['post_type']) && $request['post_type']=='sponsors') {
        $request['term'] = get_term($request['type'],'type')->name;

    }
    return $request;
} 

And this

add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
function my_restrict_manage_posts() {

    // only display these taxonomy filters on desired custom post_type listings
    global $typenow;
    if ($typenow == 'sponsors' || $typenow == 'type') {

        // create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
        $filters = array('title_sponsor', 'platin-sponsor', 'guld-sponsor', 'diamant-sponsor');

        foreach ($filters as $tax_slug) {
            // retrieve the taxonomy object
            $tax_obj = get_taxonomy($tax_slug);
            $tax_name = $tax_obj->labels->name;
            // retrieve array of term objects per taxonomy
            $terms = get_terms($tax_slug);

            // output html for taxonomy dropdown filter
            echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
            echo "<option value=''>Show All $tax_name</option>";
            foreach ($terms as $term) {
                // output each select option line, check against the last $_GET to show the current option selected
                echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
            }
            echo "</select>";
        }
    }
}

But as I said, none which has worked.

Any help or suggestions would be very much appreciated.

Thanks
Sincere
- Mestika

1 Answer 1

13

This suppose you have a custom post type "sponsors" and a custom taxonomy "types"...

function custom_meta_box() {

    remove_meta_box( 'tagsdiv-types', 'sponsors', 'side' );

    add_meta_box( 'tagsdiv-types', 'Types', 'types_meta_box', 'sponsors', 'side' );

}
add_action('add_meta_boxes', 'custom_meta_box');

/* Prints the taxonomy box content */
function types_meta_box($post) {

    $tax_name = 'types';
    $taxonomy = get_taxonomy($tax_name);
?>
<div class="tagsdiv" id="<?php echo $tax_name; ?>">
    <div class="jaxtag">
    <?php 
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'types_noncename' );
    $type_IDs = wp_get_object_terms( $post->ID, 'types', array('fields' => 'ids') );
    wp_dropdown_categories('taxonomy=types&hide_empty=0&orderby=name&name=types&show_option_none=Select type&selected='.$type_IDs[0]); ?>
    <p class="howto">Select your type</p>
    </div>
</div>
<?php
}

/* When the post is saved, saves our custom taxonomy */
function types_save_postdata( $post_id ) {
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ) ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['types_noncename'], plugin_basename( __FILE__ ) ) )
      return;


  // Check permissions
  if ( 'sponsors' == $_POST['post_type'] ) 
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
  }

  // OK, we're authenticated: we need to find and save the data

  $type_ID = $_POST['types'];

  $type = ( $type_ID > 0 ) ? get_term( $type_ID, 'types' )->slug : NULL;

  wp_set_object_terms(  $post_id , $type, 'types' );

}
/* Do something with the data entered */
add_action( 'save_post', 'types_save_postdata' );
2
  • Hey JohnnyPea, thank you SO much. It work perfectly.!
    – Mestika
    Commented Sep 15, 2011 at 6:20
  • 1
    @Mestika I am glad it works for you. Please, could you label this post as the "right answer" and vote it up if it was useful for you. Thanks Commented Sep 15, 2011 at 14:43

Your Answer

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

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