forked from Hube2/acf-filters-and-functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acf-page-granparent-location-rule.php
65 lines (60 loc) · 1.98 KB
/
acf-page-granparent-location-rule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/*
Adds a custom location rule to ACF to select page grandparent
*/
add_filter('acf/location/rule_types', 'acf_location_rules_page_grandparent');
function acf_location_rules_page_grandparent($choices) {
$choices['Page']['page_grandparent'] = 'Page Grandparent';
return $choices;
}
add_filter('acf/location/rule_values/page_grandparent', 'acf_location_rules_values_page_grandparent');
function acf_location_rules_values_page_grandparent($choices) {
// this code is copied directly from
// render_location_values()
// case "page"
$groups = acf_get_grouped_posts(array(
'post_type' => 'page'
));
if (!empty($groups)) {
foreach(array_keys($groups) as $group_title) {
$posts = acf_extract_var($groups, $group_title);
foreach(array_keys($posts) as $post_id) {
$posts[$post_id] = acf_get_post_title($posts[$post_id]);
};
$choices = $posts;
}
}
// end of copy from ACF
return $choices;
}
add_filter('acf/location/rule_match/page_grandparent', 'acf_location_rules_match_page_grandparent', 10, 3);
function acf_location_rules_match_page_grandparent($match, $rule, $options) {
// this code is with inspiration from
// acf_location::rule_match_page_parent()
// with addition of adding grandparent check
$post_grandparent = 0;
if (isset($options['page_parent']) && $options['page_parent']) {
$parent = get_post($options['page_parent']);
if ($parent->post_parent) {
$post_grandparent = $parent->post_parent;
}
} elseif (isset($options['post_id']) && $options['post_id']) {
$post = get_post($options['post_id']);
if ($post->post_parent) {
$parent = get_post($post->post_parent);
if ($parent->post_parent) {
$post_grandparent = $parent->post_parent;
}
}
}
if (!$post_grandparent) {
return false;
}
if ($rule['operator'] == "==") {
$match = ($post_grandparent == $rule['value']);
} elseif ($rule['operator'] == "!=") {
$match = ($post_grandparent != $rule['value']);
}
return $match;
}
?>