Support

Account

Home Forums Feedback get_field_ID()

Solving

get_field_ID()

  • I would really like a function that allows me to pass a post-type name and a field-name and returns the field ID

    $field_id = get_field_ID( $post_type, $field_name );

    So if I had a field called “ems_event_date” in a post type called “emsevents”, I could get the ACF field ID.

    I can use get_field_object(), but that requires a valid $post_id and I need to get the field ID when there isn’t a valid post. There has to be a way that ACF knows what fields to create on a new post.

  • This is kinda complicated as a custom field could be attached to something else than a post type. It can be attached to a specific page, a taxonomy or a lot of other things. In fact it is attached to all the posts which correspond to the type of object(s) you attach the custom field.

    That’s why (I think) the get_field_object() requires at least one $post_id to be found.

  • Then how does ACF determine what fields to display for a NEW post-type?

    There has to be some way to determine what field groups are used by a specific post type.

  • When displaying a post editor, or taxonomy editor or any form that ACF let’s you add a field group to ACF determines this by looking at the location rules for that field group and comparing them to the current “location”. When the post is saved in the admin the fields are saved to the object being saved, there isn’t any reason to check the location, acf just saves any ACF fields that are submitted using the current WP object ID.

    When getting values ACF uses the global “$post->ID” to get values and this is why you must supply a post ID for anything other than the post in “The Loop”

    In order to do what you want you would need to do a WP query for a post in that post type where the field “Exists” and then use the post ID returned from this in combination with the field name to get the field object.

    Alternately, this can be done more easily by using the field key get_field_object('field_0123456789abc');

Viewing 4 posts - 1 through 4 (of 4 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.