Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active June 23, 2016 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/8e85d4fdf8684c4b90f78c5e28d7b5c7 to your computer and use it in GitHub Desktop.
Save billerickson/8e85d4fdf8684c4b90f78c5e28d7b5c7 to your computer and use it in GitHub Desktop.
<?php
/**
* 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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment