Home › Forums › ACF PRO › Steve Jobs and Bill Gates – Relationship issue › Reply To: Steve Jobs and Bill Gates – Relationship issue
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);
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.