Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active June 14, 2016 16:46
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/6cac1b24e1a26600318b83e090e9e03b to your computer and use it in GitHub Desktop.
Save billerickson/6cac1b24e1a26600318b83e090e9e03b to your computer and use it in GitHub Desktop.
<?php
/**
* ACF Rule Values: Page Ancestor
*
* @author Bill Erickson
* @see http://www.billerickson.net/acf-custom-location-rules
*
* @param array $choices, available rule values for this type
* @return array
*/
function ea_acf_rule_values_page_ancestor( $choices ) {
// Copied from acf/core/controllers/field_group.php
// @see http://bit.ly/1Xnx44g
$post_type = 'page';
$posts = get_posts(array(
'posts_per_page' => -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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment