Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DimaMinka/71b2e2d342c6bcae8c7fe089dbff38e6 to your computer and use it in GitHub Desktop.
Save DimaMinka/71b2e2d342c6bcae8c7fe089dbff38e6 to your computer and use it in GitHub Desktop.
Get child pages relationship - Carbon Fields
/**
* Child pages filter
*
*/
function cdk_child_carbon_relationship_options( $options, $field_name ) {
if ($field_name == '_featured_articles') {
$parent_id = $_GET['post'];
$args = array(
'post_type' => 'page',
'numberposts' => -1,
'post_parent__in' => array($parent_id)
);
$posts = get_posts($args);
$post_ids = [];
if ($posts)
foreach ($posts as $post)
{
$post_ids[] = $post->ID;
}
foreach($options as $index => $page) {
if (!in_array($page['id'], $post_ids)) {
unset($options[$index]);
}
}
}
return $options;
}
add_filter('carbon_relationship_options', 'cdk_child_carbon_relationship_options', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment