Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My goal was simple --- to create a custom post type with a custom metabox with a simple drop down list of taxonomy terms instead of utilizing the tags or checkboxes. The reason was to ensure that the editor can only select a single term from the taxonomy list.

After trial and error I finally managed to figure out a way to utilize the excellent metabox creation tools I am using from WP-Alchemy (http://farinspace.com/wpalchemy-metabox/) to create my metabox and have it display the applicable terms in a dropdown list.

The problem I am having is that I can't seem to get a new selection from the dropdown menu to save the selected value.

To be clear... its important that only a single term be associated with each post in this custom post type meaning that whenever a different value is select from the dropdown menu and the post is saved it needs to ensure the previous selection is unregistered.

As mentioned I currently have it displaying the list of terms in a dropdown and I also have it correctly displaying whatever term might be associated.

Just the saving of the new values is where i am having issues.

From extensive research it "seems" like the solution involves using the wordpress "wp_set_post_terms" functions which is further explained here: http://codex.wordpress.org/Function_Reference/wp_set_post_terms However I am unsure how to utilize it correctly to achieve my desired result.

Below I have include the code I am utilizing. It's very possible there is a better way of doing this and I would appreciate whatever suggestions you guys might have might also ensure that the applicable "checks" are being done to ensure data can't be inserted by users that are not authorized.

FUNCTIONS.PHP FILE (SHOWS THE CREATION OF THE CUSTOM POST TYPE AND TAXONOMY BELOW.

//////////////////////////////////////////////////////////////////////////////
// CUSTOM POSTTYPE FOR -- SERVICES  
//////////////////////////////////////////////////////////////////////////////

add_action('init', 'services');
function services() {
 register_post_type('services', array(
  'labels' => array(
   'name'   => __('Services'),
   'singular_label'  => __('Service'),
   'new_item'   => __('Add a Service'),
   'add_new'   => __('Add a Service'),
   'add_new_item'  => __('Add a Service'),
   'edit'   => __('Edit Service'),
   'edit_item'   => __('Edit Service'),
   'view'   => __('View Service'),
   'view_item'   => __('View Service'),
   'search_items'  => __('Search Services'),
   'not_found'   => __('No Services Found'),
   'not_found_in_trash' => __('No Services Found in Trash'),
       'parent_item'  => __('Parent Service'),
   'parent_item_colon' => __('Parent Service:')
   ),
  'can_export'    => true,
  'menu_position'   => 7,
  'public'    => false,
  'show_ui'    => true,
  'publicly_queryable'  => true,
  'hierarchical'   => true,
  'query_var'    => true,
  'capability_type'   => 'post',
  'exclude_from_search'  => false,
  'supports' => array(
   'title',
   'editor',
   'revisions',
   'page-attributes'
   ),
  'rewrite' => array( 
   'slug'   => 'disability-services', 
   'with_front'   => false
   )
 ));
}

HERE IS WHERE I AM REGISTERING THE TAXONOMY I AM TRYING TO WORK WITH

// CUSTOM TAXONOMY METABOX POSTTYPE - AGE GROUPS
   register_taxonomy('theme', array('services'), array(
    'hierarchical'          => false, 
    'singular_label'            => 'Age Group', 
    'query_var'                 => 'theme',
    'public'                => true,
    'show_ui'               => true,
    'show_tagcloud'             => false,
    'show_in_nav_menus'             => true,    
    'rewrite'               => array( 'slug' => 'age-groups', 'with_front' => false ),
    'labels' => array(
        'name'          => __( 'Age Groups' ),
        'singular_name'         => __( 'Age Groups' ),
        'search_items'      => __( 'Search Age Groups' ),
        'all_items'             => __( 'All Age Groups' ),
        'parent_item'       => __( 'Parent Age Group' ),
        'parent_item_colon'         => __( 'Parent Age Group:' ),
        'edit_item'             => __( 'Edit Age Group' ), 
        'update_item'       => __( 'Update Age Group' ),
        'add_new_item'      => __( 'Add Age Group' ),
        'new_item_name'         => __( 'New Name of Age Group' ),
        'popular_items'         => __( 'Popular Age Groups' ),
        'separate_items_with_commas'=> __( 'Separate Age Groups with commas' ),
        'add_or_remove_items'   => __( 'Add or remove Age Groups' ),
        'choose_from_most_used' => __( 'Select Popular Age Groups' ), 
        ),
   ));
   wp_insert_term('Kids', 'theme');
   wp_insert_term('Teens', 'theme');
   wp_insert_term('Adults', 'theme');

THIS IS THE REMAINDER OF THE CODE I AM USING IN THE FUNCTIONS FILE AND THE CODE TO CREATE THE METABOX BASED OFF WPALECHEMY. In this attempt I tried to include 'save_filter' => "wp_set_post_terms( $post->ID, 'theme' )", with the hope this would save the applicable data but it did not.

// CUSTOM METABOX POSTTYPE - SERVICE DETAILS
   $custom_metabox_service_details = new WPAlchemy_MetaBox(array (
    'id'        => '_service_details-metaboxes',        // underscore prefix hides fields from the custom fields area
    'title'     => 'Age Groups',            // title added automatically to the custom metabox
    'types'     => array('services'),           // added only for custom post type "name-of-post-type" can also be "page" or "post"
    'context'   => 'normal',                    // same as above, defaults to "normal" but can use "advanced" or "side"
    'priority'  => 'high',                  // same as above, defaults to "high" but can use "low" as well
    'mode'  => WPALCHEMY_MODE_EXTRACT,
    'save_filter' => "wp_set_post_terms( $post->ID, 'theme' )",
    'template'  => TEMPLATEPATH . '/admin-metabox/service_details-metaboxes.php'  // contents for the meta box
    ));

I SHOULD ALSO NOTE THAT DIMAS OVER AT WPALECHMEY JUST ADDED SOME NEW CODE INTO HIS 1.3.2 VERSION OF GITHUB WHICH ALLOWS THE ABOVE CODE TO INCLUDE A

'save_filter' => "custom-function", 

This code allows you to create a custom function or I guess call a custom function which gets executed upon hitting the publish button so maybe that is the best way to save the data?

In any case, I am utilizing the following code for the custom metabox to display the actual dropdown list displaying the taxonomy terms.

<?php 
// This function gets called in edit-form-advanced.php
echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="' . wp_create_nonce( 'taxonomy_theme' ) . '" />';

// Get all theme taxonomy terms
$themes = get_terms('theme', 'hide_empty=0'); 
?>

<select name='post_theme' id='post_theme'>
// DISPLAY TERMS AS DROP DOWN OPTIONS
<?php $names = wp_get_object_terms($post->ID, 'theme'); ?>
<option class='theme-option' value='' <?php if (!count($names)) echo "selected";?>>None</option>
<?php foreach ($themes as $theme) {
 if (!is_wp_error($names) && !empty($names) && !strcmp($theme->slug, $names[0]->slug)) 
 echo "<option class='theme-option' value='" . $theme->slug . "' selected>" . $theme->name . "</option>\n"; 
 else
 echo "<option class='theme-option' value='" . $theme->slug . "'>" . $theme->name . "</option>\n"; 
}
?>
</select>

I am assuming saving the data should be simple as pie but I guess I am confusing myself on how to accomplish this.

As mentioned I would appreciate if you could also provide suggestions on the code to ensure any necessary checks are done to ensure data is saved correctly and only be the correctly authorized people.

Must thanks in advance!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

For anyone who is interested, I answered this question on a different post:

http://wordpress.stackexchange.com/questions/1794/need-help-in-saving-taxonomy-terms-85-solved-just-need-some-help

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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