From 7f8efa896767ebf51cf317e0af00d4b735454899 Mon Sep 17 00:00:00 2001 From: Anthony Talarico Date: Fri, 4 Aug 2017 11:02:23 -0400 Subject: [PATCH] adding filter for advanced custom fields adding page ancestor filter to custom fields plugin --- functions.php | 116 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/functions.php b/functions.php index 7195fed..47a0bea 100644 --- a/functions.php +++ b/functions.php @@ -30,4 +30,118 @@ define("ECONOMIC_BLOCKS", 4327); define("DEFAULT_BLOCKS", 24); define("DEFAULT_SLIDER", 21); -?> \ No newline at end of file +?> + -1, + 'post_type' => $post_type, + 'orderby' => 'menu_order title', + 'order' => 'ASC', + 'post_status' => 'any', + 'suppress_filters' => false, + 'update_post_meta_cache' => false, + )); + + if( $posts ) + { + // sort into hierachial order! + if( is_post_type_hierarchical( $post_type ) ) + { + $posts = get_page_children( 0, $posts ); + } + + foreach( $posts as $page ) + { + $title = ''; + $ancestors = get_ancestors($page->ID, 'page'); + if($ancestors) + { + foreach($ancestors as $a) + { + $title .= '- '; + } + } + + $title .= apply_filters( 'the_title', $page->post_title, $page->ID ); + + + // status + if($page->post_status != "publish") + { + $title .= " ($page->post_status)"; + } + + $choices[ $page->ID ] = $title; + + } + + } + + return $choices; +} +add_filter( 'acf/location/rule_values/page_ancestor', 'ea_acf_rule_values_page_ancestor' ); +/** + * ACF Rule Match: Page Ancestor + * + * @author Bill Erickson + * @see http://www.billerickson.net/acf-custom-location-rules + * + * @param boolean $match, whether the rule matches (true/false) + * @param array $rule, the current rule you're matching. Includes 'param', 'operator' and 'value' parameters + * @param array $options, data about the current edit screen (post_id, page_template...) + * @return boolean $match + */ +function ea_acf_rule_match_page_ancestor( $match, $rule, $options ) { + + if ( ! $options['post_id'] || 'page' !== get_post_type( $options['post_id'] ) ) + return false; + + $ancestors = get_ancestors( $options['post_id'], 'page' ); + $is_ancestor = in_array( $rule['value'], $ancestors ); + + if ( '==' == $rule['operator'] ) { + $match = $is_ancestor; + + } elseif ( '!=' == $rule['operator'] ) { + $match = ! $is_ancestor; + } + + return $match; +} +add_filter( 'acf/location/rule_match/page_ancestor', 'ea_acf_rule_match_page_ancestor', 10, 3 ); \ No newline at end of file -- 2.17.1