Support

Account

Home Forums ACF PRO Can I get a field list for a form which is "location rule" applied ?

Solved

Can I get a field list for a form which is "location rule" applied ?

  • Hi support team,

    Thanks for great plugin, first!

    I wonder that if there is a way to
    retrieve all fields filtered by all location rules which is set.
    (a result of should be varied by post_type, user, etc…)

    I need this kinda list to remove old & unnecessary fields’ postmeta values from database.

    I know get_fields().
    What I am imaging is something like get_fields_location_filtered($post_type, $user).

    Any idea or clue will be appreciated.

    Thanks in advance!

    Best regards

  • No, there is nothing built into ACF that will do this. Getting every field value for every field sorted by where they are located would be a time consuming task that under most conditions would time out.

    Let’s say we wanted to get all the values for all the posts and ignore for a minute users, terms and options. Here is the sequence of things you’d need to do.

    1. get all the post types registered on the site
    2. Loop through the post types and do a WP_Query to get all posts in each post type
    3. Loop through all the posts in each post type and get all the fields for each post

    And the above will not tell you the fields that are no longer used. It will only give you a list of the fields that are active in a field group.

    ACF does not know where fields are located unless it has a “$post_id” value to work with. This value is either assumed to be the current post ID that is being viewed or you must supply it when calling a function.

  • Hi @john Huebner,

    Thank you for your attentive reply!

    > ACF does not know where fields are located unless it has a “$post_id” value to work with.

    Then I still wonder why ACF can generate a form in ‘post-new’ screen depending on conditions of post_type, user, etc.
    Maybe I should dive into ACF core and fetch the code which does the work.

  • That’s because when you create a new post it has a post ID. WP automatically saves an autodraft of the new post and assigns a post id to it.

  • There are internal function in ACF, for example there is a function that gets the field groups based on $post_id, I forget the name of it right now and don’t want to dig, I think it’s acf_get_field_groups() and you can pass this function an array of arguments if one of them is 'post_id' => $post_id then it will return a list of field groups for that post.

    Like I said, ACF requires a post ID to get the field groups or any fields. That post ID could be one of the types that ACF uses for posts, users, terms, etc.

  • Hi @john Huebner,

    > WP automatically saves an autodraft of the new post and assigns a post id to it.

    I was not sure about it.
    Thank you for your great support!

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

You must be logged in to reply to this topic.