@paulhuisman how did you solve this?
please let me know.
I managed to make it work through JQuery,
we need to check if the input field for the element is changed, here is the code if someone needs it:
// JavaScript Document
(function($) {
$(document).ready(function(){
alert ('hejsan test ny3');
if ( $( "#acf-field_5887df9fb89a9-input" ).length ) {
alert('#acf-field_5887df9fb89a9-input exists yeyyy!' );
}
$( "#acf-field_5887df9fb89a9-input" ).change(function(e) {
alert( "Handler for .change() called." + e.val );
});
});
})(jQuery);
regards
Hi again @james,
I have read and coded this, it was pretty easy to achieve this after som researching.
The code is below (for others who gets in same situation as me).
But I still have a problem, I have 2 object_query relationship fields and I would like the second object query relationship field to be based on the first object query relationship, depending on what is chossen in the select field. How can I retrieve the selected field in the function: report_involved_family_relationship ?
Thanks for helping me out.
/*
@ This hook allows you to modify the $args array which is used to query the posts shown in the the relationship field list.
@ Custom field, get related user to Client relations
*/
function report_involved_client_relationship( $args, $field, $post_id ) {
global $wpdb;
//Query to get related client from post_meta
$related_clients = $wpdb->get_results("SELECT meta_value AS user_ids
FROM {$wpdb->prefix}postmeta
WHERE post_id = ".get_current_user_id()." AND meta_key = 'p2p_user_to_client_to' LIMIT 1");
$related_clients_arr = explode(',', $related_clients[0]->user_ids);
$args = array(
'post_type' => 'client',
'post__in' => $related_clients_arr
);
// return
return $args;
}
// filter for a specific field based on it's name
add_filter('acf/fields/post_object/query/name=report_involved_client', 'report_involved_client_relationship', 10, 3);
/*
@ This hook allows you to modify the $args array which is used to query the posts shown in the the relationship field list.
@ Custom field, get related user to family relations
*/
function report_involved_family_relationship( $args, $field, $post_id ) {
global $wpdb;
echo '<pre>';
print_r($field);
echo '</pre>';
//Query to get related family from post_meta
$related_clients = $wpdb->get_results("SELECT meta_value AS family_ids
FROM {$wpdb->prefix}postmeta
WHERE post_id = ".selected report_involved_client id." AND meta_key = 'p2p_family_to_client_to' LIMIT 1");
$related_family_arr = explode(',', $related_clients[0]->user_ids);
$args = array(
'post_type' => 'family',
'post__in' => $related_family_arr
);
// return
return $args;
}
// filter for a specific field based on it's name
add_filter('acf/fields/post_object/query/name=report_involved_family', 'report_involved_family_relationship', 10, 3);
Hi @james,
thank you for oyur reply and helping me out.
I can some coding but I am not good at WP :/
I have added this code to the functions.php file:
function my_relationship_query( $args, $field, $post_id ) {
// only show children of the current post being edited
$args['post_parent'] = $post_id;
// return
return $args;
}
// filter for a specific field based on it's name
add_filter('acf/fields/relationship/query/name=report_relations', 'my_relationship_query', 10, 3);
Now I want the report_relations field to only show the following:
I have a post meta named “p2p_user_to_client_to”, the post_id in the postmeta table refers to the userID and the meta_value refers to a custom post type named “client”.
So basically I just want the the following in human language:
Get all the rows in the table for (Logged in user == postmeta.post_id)
AND
Get the custom post types WHERE post_type == client and post_type_id == (is whatever meta_value is for the found rows in the postmeta table).
I dont know really know how to imeplemtn this to the function above since I dont know how WP works.
If you can help me out I would be very very glad.
Thanks a lot for your help @James!
regards
Ok..solved it..
Perhaps, you should add an example, it was realy confusing for me.
This is the solution:
/*
@ Format Date time picker fields
*/
function my_acf_input_admin_footer() {
?>
<script type="text/javascript">
(function($) {
// JS here
acf.add_filter('date_picker_args', function( args, $field ){
// do something to args
args['minDate'] = "-30"; //For example, "+1m +7d" represents one month and seven days from today.
args['maxDate'] = "30";
return args;
});
acf.add_filter('time_picker_args', function( args, $field ){
// do something to args
args['stepMinute'] = 5;
// return
return args;
});
})(jQuery);
</script>
<?php
}
add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
Thanks @James for your reply.
I have already done that, the problem is that ALL Clients is showing up, but I only want the related Clients to show up.
When Steve Jobs is logged in and wants to add a new Report, then only the following shoud be visible for him:
Apple
Sony
And when Bill Gates is logged in and wants to add a new Report, then only the following shoud be visible for him:
Microsoft
————-
As it is now, every Client is shown up even though I have relationship between the Clients and the Users.
Problem is as I can see, when I add a relationship field there is no option to only show relationships for the specific logged in user.
Or am I doing something wrong?
Not that I have a bi-directional relationship between the User and Client which works without any problem. I only want the related Clients to show up for the logged in user.
Thank you very much fpr helping me out of this problem.
regards
Hi @James,
I did this and I hope that they can implement this feature as soon as possible.
regards
Hi @James
I would like it to show up choices, so the User is able to choose a Client and save it to the database.
I dont know how to use and set the location rules.
I would be very glad if you/someone could help me to achieve this.
regards
John:
I think that this is an awesome plugin and should definately be added to the core of ACF 5.
I have suggested this before but nothing happened unfortunately :/
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.