Support

Account

Home Forums Backend Issues (wp-admin) Problem calling get_fields()

Helping

Problem calling get_fields()

  • When I call get_fields( $post->ID ) on a post that have not been touched by ACF, I get an empty array back. Even when post meta is present.

    I have a third-party plugin that creates posts and post meta with geospatial data. It does this using the standard WordPress postmeta functions and not ACF. So the ACF field ID post meta is never created for these posts.

    While the get_field() function works with the data no matter what, get_fields() does not.

    What I need is a way to generate the ACF field ID post meta for each field. So that get_fields() will work.

    The only way I know how to do that right now is to update_field( 'field_name', get_field( 'field_name', $post->ID ), $post->ID ) for each field. I’m really looking for a generic way to do this.

  • Assuming the update_field() call in your post works as expected (I haven’t looked into this) you should be able to loop the output from WordPress’s get_post_meta() function.

    Something like this:

    
    function update_fields_for_id( $post_id ) {
    	$fields = get_post_meta( $post_id );
    
    	foreach ($fields as $field_name => $field_value) {
    		// Don't process fields with leading underscore
    		// which are marked as private for internal use only
    		if ( 0 !== strpos( $field_name, '_' ) ) {
    			update_field( $field_name, get_field( $field_name, $post_id ), $post_id );
    		}
    	}
    }
    
Viewing 2 posts - 1 through 2 (of 2 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.