Hello John
I was able to use successfully your other post :
https://github.com/Hube2/acf-dynamic-ajax-select-example/tree/master/dynamic-fields-on-relationship
My problem now, is to populate some fields (example: title, image …) but not from 1 post but from 2 different posts.
I try to not use a relationship field but instead use a post object field, do you have an example with 2 objects field ?
Concerning the example above, do you have a json field group to test with it ?
Thank you for your help
Hello
Not working
Fixed with :
$(‘[data-key=”field_0123456″] .acf-input .acf-actions [data-event=”add-row”]’).trigger(‘click’);
Thank you
Hello
Please answer only the first question about metafields, second question fixed (code inserted in functions.php and worked)
Thank you
Hello,
I have also to use the code in the php file
<?php
new my_dynmamic_field_on_relationship();
// you should probably change the name of this class and
// modify the statement above that instantiates it
class my_dynmamic_field_on_relationship {
public function __construct() {
// enqueue js extension for acf
// do this when ACF in enqueuing scripts
add_action('acf/input/admin_enqueue_scripts', array($this, 'enqueue_script'));
// ajax action for loading values
add_action('wp_ajax_load_relationship_content', array($this, 'load_content_from_relationship'));
} // end public function __construct
public function load_content_from_relationship() {
// this is the ajax function that gets the related values and returns them
// we can use the acf nonce to verify the request
if (!wp_verify_nonce($_POST['nonce'], 'acf_nonce')) {
echo json_encode(false);
exit;
}
// check for our other required values
if (!isset($_POST['post_id']) || !isset($_POST['related_post']) || !isset($_POST['image_size'])) {
echo json_encode(false);
exit;
}
// post IDs must be integers
$post_id = intval($_POST['post_id']);
$related_post = intval($_POST['related_post']);
// get the image size to return
$image_size = $_POST['image_size'];
// you need to replace all field names in all of the following code
// with the field names of your fields
// first check to see if the selected related post is the same
// as the current value for the related post
// if it is return values that have already been saved for this post
// the 3rd parameter set to false returns unformatted value
$value = get_field('relationship', $post_id, false);
// relationship fields always returns an array
// use the first value of the array
$value = intval($value[0]);
if ($value == $related_post) {
// this post already has a related post and it is the same one just selected
// return values that were previously saved for the fields
// get the image, if any
$image = false;
$image_id = intval(get_field('relationship_image', $post_id, false));
if ($image_id) {
$image_details = wp_get_attachment_image_src($image_id, $image_size);
if ($image_details) {
$image = array(
'id' => $image_id,
'url' => $image_details[0]
);
}
}
// put all the values into an array and return it as json
$array = array(
'title' => get_field('relationship_title', $post_id),
'excerpt' => get_field('relationship_excerpt', $post_id),
'image' => $image
);
echo json_encode($array);
exit;
}
// this is a different related post
// get the post and set it up
global $post;
$post = get_post($related_post);
setup_postdata($post);
// get the excerpt
$excerpt = trim($post->post_excerpt);
if (!$excerpt) {
// the excerpt is not set, create on from the content
$excerpt = apply_filters('the_excerpt', $post->post_content);
}
// get the image if any
$image = false;
$image_id = intval(get_post_thumbnail_id($post->ID));
if ($image_id) {
$image_details = wp_get_attachment_image_src($image_id, $image_size);
if ($image_details) {
$image = array(
'id' => $image_id,
'url' => $image_details[0]
);
}
}
// put all the values into an array and return it as json
$array = array(
'title' => $post->post_title,
'excerpt' => $excerpt,
'image' => $image
);
echo json_encode($array);
exit;
} // end public function load_content_from_relationship
public function enqueue_script() {
// enqueue acf extenstion
// only enqueue the script on the post page where it needs to run
/* *** THIS IS IMPORTANT
ACF uses the same scripts as well as the same field identification
markup (the data-key attribute) if the ACF field group editor
because of this, if you load and run your custom javascript on
the field group editor page it can have unintended side effects
on this page. It is important to alway make sure you're only
loading scripts where you need them.
*/
global $post;
if (!$post ||
!isset($post->ID) ||
get_post_type($post->ID) != 'post') {
return;
}
// the handle should be changed to your own unique handle
$handle = 'my-dynamic-repeater-on-relationship';
// I'm using this method to set the src because
// I don't know where this file will be located
// you should alter this to use the correct functions
// to get the theme, template or plugin path
// to set the src value to point to the javascript file
$src = '/'.str_replace(ABSPATH, '', dirname(__FILE__)).'/dynamic-fields-on-relationship.js';
// make this script dependent on acf-input
$depends = array('acf-input');
wp_register_script($handle, $src, $depends);
// localize the script with the current post id
// we will need the current post ID to get existing
// values from the post
// you should change this object name to something unique
// will will also need to change this object name in the JS file
$object = 'my_acf_extension_object';
$data = array('post_id' => $post->ID);
wp_localize_script($handle, $object, $data);
wp_enqueue_script($handle);
} // end public function enqueue_script
} // end class my_dynmamic_field_on_relationship
?>
Where i have to add this code ? in the functions.php file ?
Thank you
Hello
Thank you for your reply, as i understand : this method works well when copying : title, image, excerpt ( standard wordpress fields) form Post A to ACF based Post B
In my case, i have also the Post A based on ACF Pro
Can you give ma an example: how can i copy a metafield based on ACP to Post B ?
Thank you in advance
Hello
When this option will be added ?
How to hide some fields on frontend form ?
Thank you
Hello,
If the email set in email field :
$email = get_field('email_de_contact', $post_id);
is of type : @gmail.com, @yahoo.fr …
The email is not received
if the email is of type @customaddress.com the email is received !
Strange, i’m not sending email to email_de_contact !
Thank you
Hello,
Thank you for your reply
It was a JS conflict from theme. Fixed by removing some uneeded functions.
Thank you
Hello,
I have send a support ticket but not have any reply !
Thank you
Hello,
This worked well for me !
function my_relationship_query( $args, $field, $post ) {
// get posts for current logged in user
$args['author'] = get_current_user_id();
return $args;
}
add_filter('acf/fields/relationship/query/name=instructors', 'my_relationship_query', 10, 3);
Thank you
Thank you for this code
Thank you very much
ACF Pro : The best plugin
Hello,
When I set the last tab end point to yes, the tab is drawn on a new line !
https://cloudup.com/cet_4jogsIi
Thank you
Hello,
Please see attached images
Thank you in advance for your help
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.