Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created June 14, 2016 17:15
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/bc2f167833f071397fa3437e13dfd584 to your computer and use it in GitHub Desktop.
Save billerickson/bc2f167833f071397fa3437e13dfd584 to your computer and use it in GitHub Desktop.
<?php
/**
* ACF Rule Match: Page Type
*
* @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_type( $match, $rule, $options ) {
// Only run for 'no_children' value
if ( 'no_children' != $rule['value'] ) {
return $match;
}
// Only run if post ID is defined and this is a page
if ( ! $options['post_id'] || 'page' != get_post_type( $options['post_id'] ) ) {
return $match;
}
$children = get_pages( array( 'child_of' => get_the_ID() ) );
if ( '==' == $rule['operator'] ) {
$match = empty( $children );
} elseif ( '!=' == $rule['operator'] ) {
$match = ! empty( $children );
}
return $match;
}
add_filter( 'acf/location/rule_match/page_type', 'ea_acf_rule_match_page_type', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment