Support

Account

Home Forums Feature Requests Update_field to automatically use field key Reply To: Update_field to automatically use field key

  • Yes indeed, not a bad solution!

    In my personal case, I’ve decided to work around the ugly field keys by defining consts for all fields, grouped in classes which matches my post types.

    Basically, I have created classes with consts for each field. As I code in Netbeans, my solution has the added benefit of auto-completion for the ACF-fields. I find the added coding overhead is quite small with my solution, although there obviously is an overhead.

    class CAR {
    	// Car - Base information
    	const REGISTRATION_NUMBER = "field_51a66ab4a253c";
    	const MAKE = "field_51a66a92a2539";
    	const MODEL = "field_51a66a9aa253a";
    	const MODEL_YEAR = "field_51a66a9fa253b";
    	
    	// Car - Geographical details
    	const ADDRESS = "field_51a66dc064cbb";
    	const POSTAL = "field_51a66dcd64cbd";
    	const CITY = "field_51a66dc664cbc";
    	const COUNTRY = "field_51a66dd664cbe";
    	const GEOLOCATION = "field_51e1d1afb965c";
    
    // ... more fields ...
    }
    
    class USER {
    	// User
    	const AVATAR_URL = "field_51e306ce37798";
    	const FACEBOOK_USER_ID = "field_51e3070737799";
    	const NEWSLETTER = "field_51e47ad26a7a6";
    }
    

    So when I want to update or get a field, I now do:

    update_field(CAR::REGISTRATION_NUMBER, $registration_number, $postID);
    

    It works quite well for me. Nonetheless, I stand by the feature request. I would have preferred simply having to write the following and not worry about extra classes and consts:

    update_field('registration_number', $registration_number, $postID);