Wordpress: How to "pull" conditional taxonomy in a selection field?

Asked

Viewed 231 times

0

To optimize the loading and improve the understanding for the user, I need to take the location hierarchically, ex:

In Wordpress the order is:

Brazil
-Santa Catarina
--Florianopolis

In the post edition page I want:

Select a Country

Select Brazil > (open field) > States > (open field) > Cities

It’s Possible?

I have the code below but only shows the country:

function custom_meta_box() {

    remove_meta_box( 'tagsdiv-local', 'post', 'side' );

    add_meta_box( 'tagsdiv-local', 'Localização', 'types_meta_box', 'post', 'side' );

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

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

    $tax_name = 'local';
    $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, 'local', array('fields' => 'ids') );
    wp_dropdown_categories('taxonomy=local&hide_empty=0&orderby=name&name=local&show_option_none=Select type&selected='.$type_IDs[0]); ?>
    <p class="howto">Selecione um País</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 ( 'post' == $_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['local'];

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

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

}
/* Do something with the data entered */
add_action( 'save_post', 'types_save_postdata' );

Thank you!

  • Do you already have the other taxonomies registered? Your code only has a dropdown to "local". Already have some javascript to make the call?

  • Yes the taxonomy is already registered to make the call, in this sequence: Parents (level 1) > State (level 2) > City > (level 3).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.