Home › Forums › General Issues › Autocomplete fields values
Hi, I’m currently workind on a code for a project, I’m a bit noob with PHP but I’m trying to work with the doc and a few PHP lessons.
Here is what I want to do :
I got a custom post about a video, the admin register the video url, author, author’s website, author’s facebook, author’sinstagram.
When he types the name of the author, i want it to be checked, then if the author already exist, then use the previous data used from a past post for this artist to fill the fields website, instagram, facebook etc…
I think i got the overall idea of how to articulate the code but…
<?php
// If the value auteur_video1 already exist, take auteur_video other fields (website, facebook, instagram) values
// and fill the curent post fields with the data.
// I / Check if value already exist
$args = array(
'post_type' => 'videos',
'meta_query' => array(
array(
'key' => 'auteur_video',
'value' => '111'
)
),
'fields' => 'ids'
);
// perform the query
$vid_query = new WP_Query( $args );
$vid_ids = $vid_query->posts;
// do something if the meta-key-value-pair exists in another post
if ( ! empty( $vid_ids ) ) {
// II / If exist then autocomplete the fields
function autocomplete_fields {
$artistsite = get_field('website_artiste', $post->ID); // get the auteur_vide1 field value
$artistfb = get_field('facebook_artiste', $post->ID); // get the auteur_vide1 field value
$artistinst = get_field('instagram_artiste', $post->ID); // get the auteur_vide1 field value
function my_acf_update_value( $value, $post_id, $field ) { // Complete the field facebook_artist with the value
// override value
$value = "$artistsite";
// return
return $value;
}
acf/update_value/name={$field_name}
add_filter('acf/update_value/name=facebook_artist', 'my_acf_update_value', 10, 3);
}
?>
I don’t expect a full answer i know it takes time and i don’t want to wast your time, but little help (link to apprepiate docs etc..) will be great.
I will update this thread as my work on this goes if it’s ok, In case it could help someone.
Thanks.
Hi @warry,
For this case, I think it will be better if you use post object field instead. You can create a custom post type using CPT UI to make it easier. You can also create some custom fields to create a new author, and use acf/save_post to save it as a custom post.
Hope this helps 🙂
You must be logged in to reply to this topic.
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.