Home › Forums › Front-end Issues › Field group for Taxonomy Term MIA › Reply To: Field group for Taxonomy Term MIA
Here’s my code for anyone that this might help.
This is my solution to specifically targeting children of parent categories:
i.e.:
(Credit to Kellen Mace for a snippet to identify the current taxonomy admin page. Link in the code.)
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class ACF_Category_Name_Location extends ACF_Location {
public function initialize() {
$this->name = 'category_name';
$this->category = __( 'Category', 'acf');
$this->label = __( 'Category Name', 'acf' );
$this->object_type = 'post';
}
public function get_values( $rule ) {
$choices = array();
// Load all category terms, loop over them and append to choices.
$terms = get_terms( array(
'taxonomy' => 'category',
'orderby' => 'name',
'hide_empty' => false,
'parent' => 0,
) );
if( $terms ) {
foreach( $terms as $term ) {
$choices[ $term->term_id ] = $term->name;
}
}
return $choices;
}
public function match( $rule, $screen, $field_group ) {
/**
* https://kellenmace.com/get-term-edited-term-php-wordpress-admin-page/
*/
// Get the term currently being edited on the edit.php screen in the admin.
global $taxnow;
if ( ! $taxnow || empty( $_GET['tag_ID'] ) ) {
return false;
}
$term_id = absint( $_GET['tag_ID'] );
$term = get_term( $term_id, $taxnow );
// Return false if not WP_Term
if ( ! $term instanceof WP_Term ) {
return false;
}
// Compare the current term parent to rule value.
$result = ( $term->parent == $rule['value'] );
// Return result taking into account the operator type.
if( $rule['operator'] == '!=' ) {
return !$result;
}
return $result;
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.