I need to insert a post via wp_insert_post and then add relevant post_meta – however, the WP admin has been customized with the excellent ACF.
I know I can add_post_meta in this way:
add_post_meta( $post_id, 'q_key_type', $q_key_type );
However, to make ACF happy, it’s required to also use the “update_field” function – like so:
update_field( "field_52c737a413e07", $q_key_type, $post_id );
This method works fine, but I can’t get a Taxonomy object to register correctly – I can get the value to save using add_post_meta – when I view the new post it does not recognise the Taxonomy – the value is empty, but in the postmeta table the value is stored correctly – what is missing is the correct field recognition.
I can save fields of the type “Text” and “User” correctly using these two methods.
Here is an image of two rows in the post_meta table – the first two are created by acf – the last two manually – they appear to be formatted identically:
I can only find one other reference to the same field name in the post_meta table – it contains an array with the following data – which seems to be the field set-up for acf:
Array (
[key] => field_52c737a413e07
[label] => Type
[name] => q_key_type
[type] => taxonomy
[instructions] =>
[required] => 0
[taxonomy] => q_key_type
[field_type] => select
[allow_null] => 0
[load_save_terms] => 1
[return_format] => object
[conditional_logic] => Array (
[status] => 0
[rules] => Array (
[0] => Array (
[field] => null
[operator] => ==
[value] =>
)
)
[allorany] => all
)
[order_no] => 3
)
So, the question is – where else is ACF saving the taxonomy data – I can’t find anything in the terms tables?
I do need to add both post_meta:
And for ACF:
And the rest of the answer “should” be covered by wp_set_object_terms:
However, this function is not fully available at the point I need it – so the answer was to create a simple replacement for this function:
Which I can call using a static class instance ( as the method is part of a separate class in my case… ):
I’ve found that the
update_field()
function actually does the job for me, I don’t know if it’s been updated since to make that happen. All you have to watch out for is if you have a single choice (select) field or one that allows multiple selections – you need to pass through an array of taxonomy ID’s if it’s multiples, basically.In example:
The
update_field()
function will handle the serialize for you automatically based on the field settings, so you don’t have to worry about anything else.I’ve used this method to update both single and multiple taxonomy fields on the same post record, and I’ve tested that I can then query those results using the ACF-recommended ways of doing so, so it’s definitely putting the data in correctly.
EDIT: one thing to bear in mind, you’ll notice I’ve turned the term_id into a string by wrapping it in quotes – ACF stores the IDs as strings in serialized arrays, so this is essential or their suggested ways to query this data won’t work (and when you go to end the field through wp-admin in the future, it will change it back to a string anyway).
Make sure you prefix the term ID of your custom taxonomy when using the
update_field()
function.Example:
The great ACF docs talk about this in the section ‘Update a value from different objects’: https://www.advancedcustomfields.com/resources/update_field/
ACF handles all the stuff around creating and syncing custom fields. No need to recreate that yourself with
add_post_meta()
etc.I lost hours down this rabbit hole, really could use a RTFM! notification every 10 minute. ð¤¦ââï¸