@hube2 I don’t think that is the issue as this error occurs when I update the custom post. I understand why there may be an error if I try to edit from the core WordPress user tab.
I actually tried to add this if but it actually caused the whole function to not run at all.
function create_user_frontend( $post_id ) {
if (get_post_type($post_id) != 'users') {
$first_name = get_field('first_name', $post_id);
$last_name = get_field('last_name', $post_id);
$username = get_field('email', $post_id);
$password = get_field('password', $post_id);
$incident = get_field('related_incident', $post_id, false);
$role = get_field ('role', $post_id);
$userdata= array (
'user_login' => $username,
'user_email' => $username,
'first_name' => $first_name,
'last_name' => $last_name,
'user_pass' => $password,
'role' => $role,
);
$user_id = wp_insert_user($userdata);
update_field('field_602ce62b11720', $incident, 'user_'.$user_id);
}
}
add_action('acf/save_post', 'create_user_frontend', 20);
@hube2 I am now getting an issue if I try to update an existing user post. Do I need to add some code for update as this update_field seems to be causing an issue for updates?
Uncaught Error: Object of class WP_Error could not be converted to string in
Ahhh makes sense thank you! So I’m setting the ID for a user not a post.
Thanks @hube2 it works! Not sure I understand why but it does 🙂
Thanks for your reply John.
I managed to make this work with the following code:
add_filter('acf/save_post', 'gallery_to_thumbnail',11,11);
function gallery_to_thumbnail($post_id) {
$gallery = get_field('images', $post_id, false);
var_dump($gallery);
var_dump($post_id);
if (!empty($gallery)) {
$image_id = $gallery[0];
set_post_thumbnail($post_id, $image_id);
}
I realised my issue isn’t with setting the featured image but setting the og:image based on the featured image for which I am using Yoast plugin. This only works once I update a post rather than create one.
I know Yoast has a filter but I am unsure how to set it using the gallery field, any ideas would be greatly appreciated:
add_filter('wpseo_opengraph_image', 'og_image');
function og_image($image) {
global $post;
if (get_field('feature_image', $post->ID)) {
$image = base_image_url(get_field('feature_image', $post->ID), null);
}
return $image;
}
I’ve just noticed it works when I update only.
@Brotsky_Pixie This works for me in terms of showing the edit form but I can’t get the edit submission div to be clickable and change the class to display the form. Anybody have any ideas what I’m doing wrong?
But that field ‘s’ isn’t an ACF field so it doesn’t have an ACF key. It is a field in my theme with the name ‘s’. I want the acf field to post the id into the post field named ‘s’. Both of the field values when I inspect are the id of the post.
Here is what happens with the first two:
1- acf title field on front-end fills the post title
2- acf description field on front-end fills the post description
What I want to happen with the third is:
3- acf post object input fills an input in the post
Please also note that the first two are text fields and are working as expected.
Hi John,
Thanks for your reply! That was stupid of me to not point out which field is the post object.
It’s the final line:
$_POST['acf']['s'] = $_POST['acf']['field_5dd54240e53b7'];
‘s’ is the name of the input in wp/admin area that I want to fill with the input from the acf field name which is the custom post object input in my acf new post form on the front-end.
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.