Support

Account

Home Forums Backend Issues (wp-admin) Fields relations – post edit page

Solving

Fields relations – post edit page

  • Hello everyone,

    I am working on a portfolio of numbered art prints project.
    I have custom posts called “projects” with a fields group composed by one “gallery” field, and one “number” field for set the total prints of each projects.

    An other fields group attached to Media Library (jpg, jpeg) with one number field to set the numbering of each project’s prints.

    I would like to set dynamicaly max limit to number on images field depending on the project to which it is attached.

    For example, if a project owns 10 prints, the number field of images attached to the project can be numbered 1 to 10.

    I tried to use “acf/prepare_field/name=print_nbr” filter with $field[‘max’] = X; but i can’t get the total number variable from the associated project in admin post edit page.

    function my_acf_init() {
    
    	//can't reach post object
    	global $post;
    	$project_total_prints = get_field('project_total_prints', $post->ID);
    		
    		//can't pass any variable
    		function my_acf_prepare_field( $field, $project_total_prints ) {
    			$field['max'] = $project_total_prints ;
    			return $field;
    		}
    		add_filter('acf/prepare_field/name=print_nbr', 'my_acf_prepare_field',10 ,2 );
    }
    
    //Others acf actions returns error or have no effect on the field
    add_action('acf/init', 'my_acf_init');

    I think it’s a wordpress environement variable problem but i’am not sure.
    any idea ?…
    ( hope my post is understandable… )

  • What type of field is your project_total_prints field? I’m assuming it is a WYSIWYG field. And are you trying to get the number of media (pictures) that have been uploaded to this field?

  • @bosoxbill “project_total_prints” is a number field
    Counting the number of images is not useful here because I can upload other images that are not part of the numbered series…

  • You don’t need that outside acf/init action to do this.

    Try this in your functions.php file (I changed the function name):

    function acf_prepare_field_set_print_max( $field ) {
    
    	$project_total_prints = get_field('project_total_prints');
    	$field['max'] = $project_total_prints;
    	return $field;
    }
    add_filter('acf/prepare_field/name=print_nbr', 'acf_prepare_field_set_print_max');
  • @bosoxbill Thx for your help
    get_field(‘project_total_prints’) return null
    I think it’s because the ‘print_nbr’ field is loaded in the gallery field type on the post edit page…

  • How about:

    function acf_prepare_field_set_print_max( $field ) {
    
    	$project_total_prints = get_field('project_total_prints');
    	$field['max'] = count($project_total_prints);
    	return $field;
    }
    add_filter('acf/prepare_field/name=print_nbr', 'acf_prepare_field_set_print_max');
  • @bosoxbill nope… count(null) = null…
    If i set manually a post ID on get_field it returns correctly the ‘project_total_prints’ value :
    $project_total_prints = get_field('project_total_prints', 105 );
    But i can’t get the post object dynamically in acf/prepare_field filter…
    global $post and $_GET[‘post’] don’t work

  • This is my JSON fields export :

    [
        {
            "key": "group_5aa5895baf14e",
            "title": "Galerie",
            "fields": [
                {
                    "key": "field_5aa58afaead97",
                    "label": "Images",
                    "name": "images",
                    "type": "gallery",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "min": "",
                    "max": "",
                    "insert": "append",
                    "library": "uploadedTo",
                    "min_width": "",
                    "min_height": "",
                    "min_size": "",
                    "max_width": "",
                    "max_height": "",
                    "max_size": "",
                    "mime_types": "jpg,jpeg"
                },
                {
                    "key": "field_5aa58c6d1857a",
                    "label": "Nombre de tirages",
                    "name": "nbr_tirages",
                    "type": "number",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "default_value": "",
                    "placeholder": "",
                    "prepend": "",
                    "append": "",
                    "min": "",
                    "max": "",
                    "step": ""
                }
            ],
            "location": [
                [
                    {
                        "param": "post_type",
                        "operator": "==",
                        "value": "projets"
                    }
                ]
            ],
            "menu_order": 0,
            "position": "normal",
            "style": "default",
            "label_placement": "top",
            "instruction_placement": "label",
            "hide_on_screen": "",
            "active": 1,
            "description": ""
        },
        {
            "key": "group_5aa58c0355671",
            "title": "Image",
            "fields": [
                {
                    "key": "field_5aa58dde15eb3",
                    "label": "Numéro de tirage",
                    "name": "no_tirage",
                    "type": "number",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "default_value": "",
                    "placeholder": "",
                    "prepend": "",
                    "append": "",
                    "min": "",
                    "max": "",
                    "step": ""
                }
            ],
            "location": [
                [
                    {
                        "param": "attachment",
                        "operator": "==",
                        "value": "image\/jpeg"
                    }
                ]
            ],
            "menu_order": 0,
            "position": "normal",
            "style": "default",
            "label_placement": "top",
            "instruction_placement": "label",
            "hide_on_screen": "",
            "active": 1,
            "description": ""
        }
    ]

    And my function.php custom post type code :

    /* CUSTOM POST TYPE */
    
        function wpm_custom_post_type() {
        	$labels = array(
        		'name'                => _x( 'Projet', 'Post Type General Name'),
        		'singular_name'       => _x( 'Projets', 'Post Type Singular Name'),
        		'menu_name'           => __( 'Projets'),
        		'all_items'           => __( 'Tous les projets'),
        		'view_item'           => __( 'Voir les projets'),
        		'add_new_item'        => __( 'Ajouter un nouveau projet'),
        		'add_new'             => __( 'Ajouter'),
        		'edit_item'           => __( 'Editer le projet'),
        		'update_item'         => __( 'Modifier le projet'),
        		'search_items'        => __( 'Rechercher un projet'),
        		'not_found'           => __( 'Non trouvé'),
        		'not_found_in_trash'  => __( 'Non trouvé dans la corbeille'),
        	);
    
        	$args = array(
        		'label'               => __( 'Projets'),
        		'description'         => __( ''),
        		'labels'              => $labels,
        		//'register_meta_box_cb'	=> 'bar',
    			//'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        		'hierarchical'        => false,
        		'public'              => true,
        		'has_archive'         => true,
        		'rewrite'			  => array( 'slug' => 'projets'),
    
        	);
    
        	register_post_type( 'projets', $args );
    
        }
    
        add_action( 'init', 'wpm_custom_post_type', 0 );
  • How about:

    function acf_prepare_field_set_print_max( $field ) {
    
    	$ID = get_the_ID();
    	$project_total_prints = get_field('project_total_prints',$ID);
    	$field['max'] = count($project_total_prints);
    	return $field;
    }
    add_filter('acf/prepare_field/name=print_nbr', 'acf_prepare_field_set_print_max');
  • @bosoxbill No, you can try with my JSON fields above

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.