Skip to content

Instantly share code, notes, and snippets.

@campusboy87
Last active April 28, 2019 17:52
Show Gist options
  • Save campusboy87/6609d7050e53ed0514524bdfa137d096 to your computer and use it in GitHub Desktop.
Save campusboy87/6609d7050e53ed0514524bdfa137d096 to your computer and use it in GitHub Desktop.
acf/location/rule_match/post_category
<?php
add_filter( 'acf/location/rule_match/post_category', 'acf_location_rules_match_category', 10, 3 );
function acf_location_rules_match_category( $match, $rule, $options ) {
$parent_slug = str_replace( 'category:', '', $rule['value'] );
$parent_term = get_category_by_slug( $parent_slug );
// Не нашлось указанной рубрики в условии по отображению
if ( ! $parent_term ) {
return $match;
}
if ( $options['ajax'] ) { // Срабатывает при AJAX запросе - выбор рубрики в админке
$select_term_ids = array_filter( $options['post_terms']['category'] );
} else { // Срабатывает при открытии страницы редактировании записи
$select_term_ids = get_post( $options['post_id'] )->post_category;
}
// Проверяем, являются ли выбранные рубрики дочерными к выбранной рубрики в условии отоюражения
foreach ( $select_term_ids as $select_term_id ) {
if ( cat_is_ancestor_of( $parent_term, $select_term_id ) ) {
return true;
}
}
return $match;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment