Support

Account

Home Forums Backend Issues (wp-admin) How to load Post Type fields with data from a remote API

Solved

How to load Post Type fields with data from a remote API

  • I need to bring the information of a Post Type from a Remote URL that show a json file.

    This information will be loaded into WP-Admin to allow user selection when Filling in custom fields on a page.

    Sample of remote url(json)

    {
        "id": 1,
        "nome": "Tribunal de Justiça do Estado do Acre",
        "sigla": "TJAC",
        "unidades": [{
                "id": 2,
                "nome": "Acrelândia",
                "sigla": "AL",
                "unidades": [{
                        "id": 24,
                        "nome": "Direção do Foro",
                        "sigla": "ALDFO",
                        "unidades": [
    
                        ],
                        "tipo": "ADMINISTRATIVA"
                    },
                    {
                        "id": 25,
                        "nome": "Vara Única",
                        "sigla": "ALVAR",
                        "unidades": [
    
                        ],
                        "tipo": "VARA"
                    }
                ],
                "tipo": "COMARCA"
            },
            {
                "id": 3,
                "nome": "Assis Brasil",
                "sigla": "AB",
                "unidades": [{
                        "id": 26,
                        "nome": "Direção do Foro",
                        "sigla": "ABDFO",
                        "unidades": [
    
                        ],
                        "tipo": "ADMINISTRATIVA"
                    },
                    {
                        "id": 27,
                        "nome": "Vara Única",
                        "sigla": "ABVAR",
                        "unidades": [
    
                        ],
                        "tipo": "VARA"
                    }
                ],
                "tipo": "COMARCA"
            }
        }
    

    My Custom Fields Created in ACF

    if( function_exists('acf_add_local_field_group') ):
    
    acf_add_local_field_group(array(
    	'key' => 'group_6100ec8d1bf79',
    	'title' => 'Lotação',
    	'fields' => array(
    		array(
    			'key' => 'field_6100f04affa8f',
    			'label' => 'Nome',
    			'name' => 'nome',
    			'type' => 'text',
    			'instructions' => 'Nome da Unidade',
    			'required' => 1,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => 'Nome da Unidade',
    			'prepend' => '',
    			'append' => '',
    			'maxlength' => '',
    		),
    		array(
    			'key' => 'field_6100f3c5ffa91',
    			'label' => 'Sigla',
    			'name' => 'sigla',
    			'type' => 'text',
    			'instructions' => 'Informar a Sigla da Unidade',
    			'required' => 1,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '',
    			'append' => '',
    			'maxlength' => 6,
    		),
    		array(
    			'key' => 'field_6100f076ffa90',
    			'label' => 'Comarca',
    			'name' => 'comarca',
    			'type' => 'taxonomy',
    			'instructions' => '',
    			'required' => 1,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'taxonomy' => 'comarca',
    			'field_type' => 'checkbox',
    			'add_term' => 0,
    			'save_terms' => 0,
    			'load_terms' => 0,
    			'return_format' => 'id',
    			'multiple' => 0,
    			'allow_null' => 0,
    		),
    		array(
    			'key' => 'field_6100f421ffa92',
    			'label' => 'Tipo',
    			'name' => 'tipo',
    			'type' => 'select',
    			'instructions' => 'Selecionar Administrativo | Primeiro Grau | Segundo Grau',
    			'required' => 1,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'choices' => array(
    				'ad' => 'Administrativo',
    				'pg' => 'Primeiro Grau',
    				'sg' => 'Segundo Grau',
    			),
    			'default_value' => 'ad',
    			'allow_null' => 0,
    			'multiple' => 0,
    			'ui' => 0,
    			'return_format' => 'array',
    			'ajax' => 0,
    			'placeholder' => '',
    		),
    		array(
    			'key' => 'field_61035043400f1',
    			'label' => 'Id Parent',
    			'name' => 'id_parent',
    			'type' => 'text',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '',
    			'append' => '',
    			'maxlength' => '',
    		),
    	),
    	'location' => array(
    		array(
    			array(
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'lotacao',
    			),
    		),
    	),
    	'menu_order' => 0,
    	'position' => 'normal',
    	'style' => 'default',
    	'label_placement' => 'top',
    	'instruction_placement' => 'label',
    	'hide_on_screen' => '',
    	'active' => true,
    	'description' => '',
    ));
    
    endif;
    

    I need to load the api data into the ACF fields, to allow editing the Page Fields in WP Admin.

    How do I implement this?

  • From what I can make out you have multiple fields that can each have multiple possible values.

    Need more information on how you are expecting the admin display and editing of these values to work.

  • I need to load the post Type with the data from the external URL, which brings a json with the data (fist code Sample of remote url(json))

    My data is:

    Comarca > Unidade > Setor

    I need tó load a data from json URL to be selected in the Backend

    `
    {
    “id”: 1,
    “nome”: “Tribunal de Justiça do Estado do Acre”,
    “sigla”: “TJAC”,
    “comarcas”: [{
    “id”: 2,
    “nome”: “Acrelândia”,
    “sigla”: “AL”,
    “unidades”: [{
    “id”: 24,
    “nome”: “Direção do Foro”,
    “sigla”: “ALDFO”,
    “setor”: [{
    “id”: 28,
    “nome”: “Vara Cível”,
    “sigla”: “ACVCI”,
    “tipo”: “VARA”
    },
    {
    “id”: 30,
    “nome”: “Vara Criminal”,
    “sigla”: “ACVCR”,
    “tipo”: “VIRTUAL”
    }

    ],
    “tipo”: “ADMINISTRATIVA”,
    ]
    }
    [{

    “id”: 2,
    “nome”: “Rio Branco”,
    “sigla”: “RB”,
    “unidades”: [{
    “id”: 24,
    “nome”: “Direção do Foro”,
    “sigla”: “ALDFO”,
    “setor”: [{
    “id”: 28,
    “nome”: “Central de Execução de Penas e Medidas Alternativas”,
    “sigla”: “BRCEPAL”,
    “tipo”: “VARA”
    },
    {
    “id”: 30,
    “nome”: “Juizado Especial Criminal”,
    “sigla”: “BRJECRIM”,
    “tipo”: “VIRTUAL”
    }

    ],
    “tipo”: “ADMINISTRATIVA”

    }]
    }]
    }
    }
    `

    Field “sertor” => receive [nome] after selecting [comarca] and [unidade]
    Field “sigla” => receive [sigla]

    I’m researching an implementation model that makes this possible.

  • You did not really answer my question about more detail.

    What I am assuming is that

    1. You load data into a field that can be selected
    2. Based on this selection you add choices to a second field or multiple other fields for selection
    3. Repeat if additional fields are dependent on the selection of the first field

    I have some examples of doing this here https://github.com/Hube2/acf-dynamic-ajax-select-example

    Basically what you are looking at is building a JavaScript/AJAX application that does the requests and population of choices in addition to adding PHP that loads the initial choices of the first field.

    What I would do is that I would create a cusome post type to hold posts returned by the API and then I’d probably use a post object field to allow the user to select on of those posts rather than try to store all of this information in fields.

  • I’m having trouble getting the [json url] object to include as options in the post type.

    Another idea would be to create with Post Types and Taxonomy, but it would need to make the relation of the CPT to allow the selection of the First Level – Parent (Unit) and after Loading the Unit in the Select Field, it loads a second Select with the Second Level Options – Child (Sector) allowing the selection of only the Sector of the Parent Unit.

    Do you have any examples of how to implement this?

    1 – I need to create two CTPs [Unit] and [Sector];
    2 – The CTP [Unit] will have a Child CPT [Sector] (How to set this Hierarchy, to allow the selection of the child?)
    3 – I have a Taxonomy [Counties] (Similar to Cities), which makes it possible to separate the [units] by [Counties]

    Did you understand my problem?

  • I use a dropdown list with manually entering data.

  • Other than that, one can find several post type that is available by default in WordPress installation.

    Post – blog post
    Page – static page
    Attachment – attached media
    Revision – post revision
    Navigation Menu – nav menu

    To create a custom post type for any particular theme on WordPress, navigate to the function.php file from your WordPress theme directory then add the following code to it.

  • If it were possible to solve it with the standard WordPress posts, I wouldn’t be on the ACF Forum asking, do you agree?

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

You must be logged in to reply to this topic.