Home › Forums › General Issues › Relationship field query customization
Hi. I need some help with the relationship field filter. I have created a field group that I want to display as a front-end form. One of the fields in that group is a relationship field named ‘production_event_recurring_link’. It will query and display all custom posts with the post type ‘production-recurring’. But since this is a front end form, I only want the user to select from the posts in that custom post type that they posted. Therefore, I’m not filtering based on meta, but simply post_author. Can someone help me modify the $args array (if it even is an array) to accomplish this, and confirm that I am using the correct field name in the filter based on the info above? Much appreciated!
<?php
function my_acf_result_query( $args, $field, $post )
{
// eg from https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
$args['meta_query'] = array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
)
);
return $args;
}
// acf/fields/relationship/result/name={$field_name} - filter for a specific field based on it's name
add_filter('acf/fields/relationship/query/name=production_event_recurring_link', 'my_acf_result_query', 10, 3);
I did something very similar recently. The code below basically queries the field ‘events’ for a list of events the user is associated with then adds the list to an array to use with the ‘include’ argument in the wp_query. Hope this snippet points you in the right direction
function my_post_object_results_for_specific_user( $args, $field, $post )
{
global $current_user, $wp_roles; get_currentuserinfo();
$user_id = $current_user->ID;
$events = get_field('events', 'user_'.$user_id );
$event_info = array();
foreach( $events as $event){
$event_info[] = $event->ID;
}
$args['include'] = $event_info;
return $args;
}
PS. You can get the post author from the $post object in the function I’m sure.
The topic ‘Relationship field query customization’ 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.