I have a custom post type with a post object field and a number of other fields.
I need to pre-populate some of the fields when the user creates a new post, including the post object field.
This used to work fine but then the post object field stopped accepting the pre-populated value.
At the moment, as part of the debugging effort, I am using this piece of code in functions.php:
add_action( 'load-post-new.php', 'wpse8650_post_new' );
function wpse8650_post_new()
{
add_action( 'wp_insert_post', 'wpse8650_wp_insert_post_default' );
}
function wpse8650_wp_insert_post_default( $post_id )
{
if ($_REQUEST["new_post_prefill"] == "yes")
{
// DROPDOWN, WORKS
add_post_meta( $post_id, 'type', "Short conversation" );
// TEXT FIELD, WORKS
add_post_meta( $post_id, 'customer', "Imaginary customer name" );
// POST OBJECT FIELD, DOESNT WORK
add_post_meta( $post_id, 'parent_post', 1234);
}
}
The fields “type” and “customer” are pre-filled with the strings I specified in the code above, but the post object field (parent_post
) does not accept the post ID I pass to it (which is 100% a correct, functioning post ID which I can select manually in the post object menu and save – it’s just the automatic pre-populating that doesn’t work).
The post object field is configured to return the post ID rather than the post object.
As I said, it used to work for the post object field until a while ago. Unfortunately I do not know what changed.
Any ideas on why this would not work?
I just created another post object field in the same form and that works fine: I can do a add_post_meta( $post_id, 'temporary_second_post_object_field', $_REQUEST['my_post_id'] );
and the field assumes that value with no problems!
I suppose deleting the original field and recreating it might work, but I can’t really afford to do that, I’ve got hundreds of values in it that I’d have to painstakingly restore somehow…. also I suppose this might happen again any time.
Could this be some obscure bug, then?
OK – not sure why, but ditching add_post_meta()
/ update_post_meta()
in favour of ACF’s own update_field
appears to work fine!
add_action( 'load-post-new.php', 'wpse8650_post_new' );
function wpse8650_post_new()
{
add_action( 'wp_insert_post', 'wpse8650_wp_insert_post_default' );
}
function wpse8650_wp_insert_post_default( $post_id )
{
if ($_REQUEST["new_post_prefill"] == "yes")
{
update_field('type', "Short conversation", $post_id );
update_field( 'customer', "Imaginary customer name", $post_id );
update_field('parent_post', 1234, $post_id );
}
}
The reason it does not work is that you are not creating a acf field reference when using update_post_meta(). Without the field key reference ACF does not know that the value you entered is for a relationship field. When you use update_field() acf creates the key reference.
@pekka I’m trying to accomplish pretty much the same thing.
I have a repeater field with 2 subfields (1 of them is a Post Object field) and I want to pre populate them when creating a new post.
What was the complete code you used?
PS. Do I need the code within ‘load-post-new.php’ too?
tnx
The topic ‘Pre-populating post object field no longer works, while other fields do’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.