5

I have a custom post type named "Designer" Each posts will be using different unique Advanced Custom Fields as each posts has unique templates.With the below code I am able to give rules for each posts in Designer post type and save but the custom fields are not displaying on post edit pages on backend. Normally this code should ork but no idea what happend to the code

Please Help.

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' => 'designer',
                '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;
}

//MATCH THE RULE
add_filter('acf/location/rule_match/cpt_parent',              'acf_location_rules_match_cpt_parent', 10, 3);
function acf_location_rules_match_cpt_parent( $match, $rule, $options )
{
    global $post;
    $selected_post = (int) $rule['value'];

    // post parent
    $post_parent = $post->post_parent;
    if( $options['page_parent'] ) {

        $post_parent = $options['page_parent'];

    }

    if ($rule['operator'] == "=="){
        $match = ( $post_parent == $selected_post );
    }
    elseif ($rule['operator'] != "!="){
        $match = ( $post_parent != $selected_post );
    }

    return $match;
}

enter image description here

JustCarty
  • 3,839
  • 5
  • 31
  • 51
Vinit Sinkar
  • 99
  • 2
  • 10

2 Answers2

7

Your Artist Collection field group is set to only appear on one post, the post Designer Post 1 which is a Designer Post type.

I don't understand what all the code is for? Just create a different field group for each post that needs a different field group and a separate rule for each.

Ok sorry I understand the issue now and I have recreated the issue on my local install.

On the line of code below you are looking for the post_parent but I think you should be looking for the ID.

I changed this:

$post_parent = $post->post_parent;

to this:

$post_parent = $post->ID;

and it's working for me.

Paul
  • 1,412
  • 1
  • 9
  • 19
  • Yes it is set to appear only on one post which is Designer Post 1.But the fields are not displaying on Designer Post 1 editor.All the screen options are ticked – Melvin Apr 04 '17 at 13:43
  • I'm guessing the code you posted is to show "Designer Post" as an option in the first rule dropdown but if you remove that code you should be able to find Designer Post 1 under Post is equal to Designer Post 1 – Paul Apr 04 '17 at 14:23
  • I have 1000s of posts which is feeling difficult to get the designer post under posts.So I added the above code to filter based on custom post type – Melvin Apr 05 '17 at 06:29
-1

If I understand your problem correctly, in wp-admin post edit page click on screen options on the upper right corner. In the menu that appears make sure the Custom fields is selected. This will make the custom fields appear for edit.

Stavros Angelis
  • 962
  • 5
  • 8