Skip to content

Instantly share code, notes, and snippets.

@obiPlabon
Last active January 25, 2018 19:07
Show Gist options
  • Save obiPlabon/de3e1afcd9056dfede35b25cf65b1540 to your computer and use it in GitHub Desktop.
Save obiPlabon/de3e1afcd9056dfede35b25cf65b1540 to your computer and use it in GitHub Desktop.
Restrict hierarchical taxonomy term relation to a certain level.
<?php
/**
* Restrict category term relation to only parent-child
*
* @see https://wpseek.com/hook/post_edit_category_parent_dropdown_args/
* @see https://wpseek.com/hook/taxonomy_parent_dropdown_args//
*
* @param array $args
* @return array Modified arguments
*/
function op_restrict_taxonomy_term_relation( $args ) {
if ( 'category' === $args['taxonomy'] ) {
$args['depth'] = 1;
}
return $args;
}
add_filter( 'post_edit_category_parent_dropdown_args', 'op_restrict_taxonomy_term_relation' );
add_filter( 'taxonomy_parent_dropdown_args', 'op_restrict_taxonomy_term_relation' );
<?php
/**
* Restrict hierarchical taxonomy term relation to certain level
*
* For instance:
* 1 for parent - child relation
* 2 for grand parent - parent - child / grand child relation
*
* @see https://wpseek.com/hook/post_edit_category_parent_dropdown_args/
* @see https://wpseek.com/hook/taxonomy_parent_dropdown_args//
*
* @param array $args
* @return array Modified arguments
*/
function op_restrict_taxonomy_term_relation( $args ) {
/**
* Taxonomy name to compare with
* or which taxonomy relation you wanna change
*
* @var string $taxonomy
*/
$taxonomy = '';
/**
* Taxonomy relation depth
*
* @var int $depth
*/
$depth = 0;
if ( $taxonomy === $args['taxonomy'] ) {
$args['depth'] = $depth;
}
return $args;
}
add_filter( 'post_edit_category_parent_dropdown_args', 'op_restrict_taxonomy_term_relation' );
add_filter( 'taxonomy_parent_dropdown_args', 'op_restrict_taxonomy_term_relation' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment