Skip to content

Instantly share code, notes, and snippets.

@puredazzle
Last active September 7, 2018 08:41
Show Gist options
  • Save puredazzle/cde2a247327b7c94db8f08e7abac9949 to your computer and use it in GitHub Desktop.
Save puredazzle/cde2a247327b7c94db8f08e7abac9949 to your computer and use it in GitHub Desktop.
Advanced Custom Fields: Location rule for query page by parent parent template. – https://www.advancedcustomfields.com/resources/custom-location-rules/
<?php
add_filter('acf/location/rule_types', function($choices) {
$choices['Page']['parent_template'] = 'Page Parent Template';
return $choices;
});
add_filter('acf/location/rule_values/parent_template', function($choices) {
$templates = wp_get_theme()->get_page_templates();
$choices = array_merge($choices, $templates);
return $choices;
});
add_filter('acf/location/rule_match/parent_template', function($match, $rule, $options) {
$pageParent = null;
if (isset($options['page_parent']) && $options['page_parent']) {
$pageParent = $options['page_parent'];
unset($options['page_parent']);
} elseif (isset($options['post_id']) && $options['post_id']) {
$post = get_post($options['post_id']);
$pageParent = $post->post_parent;
}
if (!$pageParent || $pageParent <= 0) {
return false;
}
$template = get_page_template_slug($pageParent);
if ($rule['operator'] == '==') {
$match = $rule['value'] === $template;
} elseif ($rule['operator'] == '!=') {
$match = $rule['value'] !== $template;
}
return $match;
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment