Support

Account

Home Forums Feature Requests Add condition: page parent for custom post types Reply To: Add condition: page parent for custom post types

  • Created a workaround for this by adding a new selection called “custom post types”

    If anyone else need it, or have a suggestion on how to improve it, feel free to hollar 🙂

    add_filter('acf/location/rule_types', 'acf_location_rules_types');
    function acf_location_rules_types( $choices )
    {
        $choices['Custom Post types']['cpt_parent'] = 'Custom post type parent';
     
        return $choices;
    }
    add_filter('acf/location/rule_values/cpt_parent', 'acf_location_rules_values_cpt_parent');
    function acf_location_rules_values_cpt_parent( $choices )
    {
    	$args = array(
    		'hierarchical' => true,
    		'_builtin' => false
    	);
        $posttypes = get_post_types( $args );
     
        if( $posttypes )
        {
            foreach( $posttypes as $posttype ):
            
    if( $posttype != 'acf' ):
    $args = array(
    'post_type' => $posttype,
    'posts_per_page' => -1,
    'post_status' => 'publish'
    );
    $customposts = get_posts( $args );  
    if ( $customposts  ) {
    foreach( $customposts as $custompost ){
    $choices[ $custompost->ID] = $custompost->post_title;
    }
    }
    endif;
            endforeach;
        }
     
        return $choices;
    }