Home › Forums › General Issues › Default image into ACF Field
Hi all,
I have been tearing my hair out with this one for a couple of days now.
I would like to be able to check whether an image exists in an ACF field and if not populate the field with a default image.
However, since I am making some other changes in functions.php just before save I would like to do this in the functions.php file rather than in the front end.
Could anyone give me a steer how I could achieve this – I have posted code below where I am currently making changes to the post before submission so it would be great of the code coukld be included in this.
// make changes before saving new key details (custom post type)
add_action( 'acf/save_post', 'set_title', 20 );
function set_title( $post_id ) {
$post_type = get_post_type( $post_id );
$post_author_id = get_post_field( 'post_author', $post_id );
if ( 'key_details' == $post_type ) {
$title = get_the_author_meta( 'display_name', $post_author_id);
$data = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => sanitize_title( $title ),
);
wp_update_post( $data );
}
}
I have now slightly amended the code to :-
$default_image = update_field('_thumbnail_id_pyjamas', 400);
//$values = get_fields( $post_id );
// var_dump($default_image);
//die('VALUES');
$data = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => sanitize_title( $title ),
'_thumbnail_id_pyjamas'=> $default_image
);
Still no joy.
Can anyone see what I am doing wrong?
Thanks
UPDATE:
It seems the issue is that this doesn’t work if I set the flag in the field group to use the image as the featured image. I can’t therefore quite achieve what I need which is:-
1. If no image has been added to the custom post then populate the ACF Field and the featured image with the default image.
2. If an image is uploaded to the custom post then populate the ACF field and the featured image with the uploaded image.
The second part works by just configuring the image field to use the uploaded image as the featured image but then requirement 1 doesn’t work.
Any pointers would be great.
Thanks
The topic ‘Default image into ACF Field’ 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.