Skip to content

Commit

Permalink
Merge pull request #8 from skoldin/improve/showOnlyBoundTaxTerms
Browse files Browse the repository at this point in the history
Include to the categories dropdown only the terms the posts belongs to
  • Loading branch information
mtekk committed Nov 5, 2015
2 parents ab23575 + 3a81d6f commit 139936a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions order-bender.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,31 @@ function meta_boxes($post_type, $post)
*/
function primary_taxonomy_term_meta_box($post, $callback_args)
{
global $post;

//Grab our taxonomy from the args
$taxonomy = $callback_args['args'][0];

// get all the terms the post belongs to
$terms = wp_get_post_terms( $post->ID, $taxonomy->name);
$used_terms = array();

// loop through the terms and collect their ids in the $used_terms array
foreach($terms as $term) {

// go up the hierarchy and if has parent, add the parent too to keep the hierarchical view
$parent_id = $term->parent;

if($parent_id) {
while($parent_id) {
$used_terms[] = $parent_id;
$parent_term = get_term($parent_id, $taxonomy->name);
$parent_id = $parent_term->parent;
}
}
$used_terms[] = $term->term_id;
}

//Nonce this bad boy up
wp_nonce_field($this->plugin_basename, $this->unique_prefix . '-' . $taxonomy->name . '-prefered-nonce');
$pref_id = get_post_meta($post->ID, $this->unique_prefix .'_' . $taxonomy->name . '_prefered', true);
Expand All @@ -92,6 +115,7 @@ function primary_taxonomy_term_meta_box($post, $callback_args)
'show_option_none' => __( '— Select —' ),
'option_none_value' => '0',
'selected' => $pref_id,
'include' => $used_terms,
'taxonomy' => $taxonomy->name));
}
/**
Expand Down

0 comments on commit 139936a

Please sign in to comment.