Home – Download › Forums › Custom Fields › acf_get_fields | get_fields | get_filed | Advanced Custom Fields | function
- This topic has 0 replies, 1 voice, and was last updated 3 years, 9 months ago by admin.
-
AuthorPosts
-
-
February 24, 2021 at 11:50 am #638adminKeymaster
Advanced Custom Fields (ACF) acf_get_fields function return field name, label and key etc get_fields() function return all data with field name.
We can list all fields and value for post type also by get_fields( $post_id ).
The acf_get_fields will return fields. This function not attached to posts.
The acf_get_fields Function Example:-
Above image output as bellow. We can copy group id from wp-admin > Edit field group > URL post={id}
$fields = acf_get_fields( $field_group_id );
Output:-
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758[{"ID":18,"key":"field_60308081ea3a7","label":"Demo 1","name":"subject-homenaje","prefix":"acf","type":"textarea","value":null,"menu_order":0,"instructions":"","required":1,"id":"","class":"","conditional_logic":0,"parent":17,"wrapper":{"width":"","class":"","id":""},"default_value":"","placeholder":"","maxlength":"","rows":"","new_lines":"","_name":"subject-homenaje","_valid":1},{"ID":1567,"key":"field_6030c6b5a8dd0","label":"Demo 2","name":"subject-celebracion","prefix":"acf","type":"textarea","value":null,"menu_order":1,"instructions":"","required":1,"id":"","class":"","conditional_logic":0,"parent":17,"wrapper":{"width":"","class":"","id":""},"default_value":"","placeholder":"","maxlength":"","rows":"","new_lines":"","_name":"subject-celebracion","_valid":1}]Example – get_fields() :-
$fields = get_fields( $post_id );
It will return saved post ACF fields name and the corresponding value.
API URL: http://example.com/wp-json/acf/fileds-by-post-id/{post-id}
Change {post-id} with your post id.
It is a custom ACF API example code. Just copy-paste the below code to your functions.php then navigate the API URL.
We can use this function for WooCommerce products or any other custom post type or page ACF data.
We can make a request for API URL from the front end by jQuery ajax or fetch the javascript function.
1234567891011121314function acf_custom_fileds_by_post_id( $data ){$post_id = $data['id'];$fields = get_fields( $post_id );return $fields;}add_action( 'rest_api_init', function () {register_rest_route( 'acf/', '/fileds-by-post-id/(?P<id>\d+)', array('methods' => 'GET','callback' => 'acf_custom_fileds_by_post_id',) );} );This will return the field name and value as an array. It will return false, if not exist ACF fields for the current post.
ACF get_field Function-
The ACF get_field function returns the value of the custom field.
$value = get_field( 'field_name', $post_id );
[embed]https://youtu.be/pG5EFSLnqG4[/embed]
Feel free to PM or chat. We are ready to help you 🙂
-
-
AuthorPosts
- You must be logged in to reply to this topic.