Support

Account

Home Forums General Issues Relationship Field Filter by \'last_name\'

Solving

Relationship Field Filter by \'last_name\'

  • I am either just missing it or I am attempting to do something in the wrong manner:

    SCENARIO: I have 2 Custom Posts Types

    1. Classes
    2. School Directory (2 Position Types – TEACHER & STAFF via radial select).

    The Classes can have 1 or more teachers assigned to them on the class page so I have set a RELATIONSHIP field (select_teacher) that grabs the Directory List. Return Format is set to POST OBJECT.

    MY GOAL:

    1. Only show (filter) list that returns ‘position_type’ teacher only.
    2. Order the list by the ‘last_name’ of the Teacher. In Directory I have 2 custom Fields ‘first_name’ / ‘last_name’.

    I am using the filter – “add_filter(‘acf/fields/relationship/query/key” to hook in some new $args into the relationship field via its “key”.’

    SO THE ISSUE:

    APPROACH #1: This attempt returns the list filtered for just the teacher position type but the order on the ‘last_name’ does not work:

    add_filter('acf/fields/relationship/query/key=field_565de210b1c8c', 'exclude_staff', 10, 3);
    
    function exclude_staff( $args, $field, $post )
    
    	{
    	    $args['meta_query'] = array(
                                          'post_type' => 'dwb_directory',
                                          'meta_key' => 'last_name',
    	                              'orderby' => 'meta_value',
    	                              'order' => 'ASC',
    	                              'suppress_filters' => false,
    	                              'paged' => $paged,
                                   'meta_query' => array(
                                        array(
                                             'key' => 'position_type',
                                             'value'  => 'teacher',
                                             'compare'  => 'LIKE'
                                        )
                                   )
                             );
        	return $args;
    	}
    

    APPROACH #2: This attempt returns the list filtered for just the teacher position type and the order on the ‘last_name’ does work. HOWEVER, the Search function on the Relationship field is funky. It only underlines the search paramters. The full list remains in view even if it is not part of the search query.

    It seems removing ‘[meta_query]’ from the $args parameter is what effects the Search functionality.

    
    function exclude_staff( $args, $field, $post )
    	
    	{
    	    $args = array(
                            'post_type' => 'dwb_directory',
                            'meta_key' => 'last_name',
    	                'orderby' => 'meta_value',
    	                'order' => 'ASC',
    	                'suppress_filters' => false,
    	                'paged' => $paged,
                    'meta_query' => array(
                           array(
                               'key' => 'position_type',
                               'value'  => 'teacher',
                               'compare'  => 'LIKE'
                             )
                           )
                        );
    
     	      return $args;
    
    	}
    

    Any thoughts or insights would be greatly appreciated….

    ~Jeffrey

  • Ugh… i did not format Approach #2 code… here that is:

    	add_filter('acf/fields/relationship/query/key=field_565de210b1c8c', 'exclude_staff', 10, 3);
    
    	function exclude_staff( $args, $field, $post )
    	
    		{
    			$args = array(
                                        'post_type' => 'dwb_directory',
                                        'meta_key' => 'last_name',
    	                            'orderby' => 'meta_value',
    	                            'order' => 'ASC',
    	                            'suppress_filters' => false,
    	                            'paged' => $paged,
                                     'meta_query' => array(
                                                array(
                                                     'key' => 'position_type',
                                                     'value'  => 'teacher',
                                                     'compare'  => 'LIKE'
                                        )
                                   )
                             );
    
     	      return $args;
    
    	}
    
  • The filter for the relationship query has some issues. Some things you can do and some things just break it. The problem usually happens when you try to order the fields. Filtering usually works find. The problem happens when ACF attempts to group the posts.

    If you really need to order the posts by a specific field I would suggest using a select field and dynamically generating the choices for the field yourself. http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

  • John,

    Thank you so much for the insight and guidance. I have gleamed from further research that the filters are iffy in the order actions as you mentioned.

    In my case I have a ‘first_name’ and ‘last_name’ CFs. My goal is to inject a Relationship field that lets the user select the Teacher and of course order that list by ‘last_name’. Obviously the Relationship field is pulling and utilizing the title for presentation. I am not even sure if i can change this. Not really my issue here.

    As everything stands right now everything works outside of the order. I can see how the suggested solution might be able to work for me but I would love just a little clarification. I am brand new to ACF so i appreciate any further guidance if possible.

    Q: But are you saying I should dynamically have the ‘last_name’and/or ‘first_name’ populate a Select Field and then have that be the new relationship field (ie ‘first_and_last_name’)?

    Q: If so then does that infer i would set the order of the list BEFORE the Relationship pulls the data in from that dynamic Select field?

    Again, I want to thank you John for taking the time to guide me in the issue.

    ~Jeffrey

  • Actually, you can adjust what is displayed by using this filter http://www.advancedcustomfields.com/resources/acf-fields-relationship-result/

    If you skip trying to order them by the fields that would work.

    For my original suggestion, basically you’d need to do all the work yourself with both querying post and ordering them, and on top of that all you would get is an array of IDs and would not be about to get post objects. Using a select field is overall far more complicated on the back end and front end.

  • So to be clear….

    “If you skip trying to order them by the fields that would work.”

    I’m sorry but that confuses me somewhat. Are you saying John that my only real option in using the “acf/fields/relationship/result” filter is to return IDs?

    In the meantime I did try out the result filter and as you can see it is pulling the “LAST_NAME, FIRST_NAME” FIELDS (see attached pic).

    So is it possible for

    1) Remove the Post Title so only my query results are showing?
    2) Order on the Query

    function my_relationship_result( $result, $object, $field, $post )
    	{
    
    		
    	// load a custom field from this $object and show it in the $result
    		$lname = get_field('last_name', $object->ID);
    		$fname = get_field('first_name', $object->ID);
    
    		$result .= ' [' . $lname . ', ' . $fname . ']' ;
    
    		return $result;
    
    	}
    
    add_filter('acf/fields/relationship/result/key=field_565de210b1c8c', 'my_relationship_result', 10, 4);
    
  • No, you can use acf/fields/relationship/result to return “last name, first name” to display in the select field if you choose, like you posted. But I don’t think you can order them at that point, just change what’s being displayed.

  • Thanks John,

    I’ll poke my head into some querying and/or some pre load options. But to wrap this thread up would you know of a way to hide the default “post_title”?

    I appreciate all of your guidance on this. I learned something new along the way.

    Regards,
    ~Jeffrey

  • Looking at the filter you posted above

    I also changed the variable names to better reflect what is getting passed to your function.

    
    add_filter(
      'acf/fields/relationship/result/key=field_565de210b1c8c',
      'my_relationship_result', 10, 4);
    
    function my_relationship_result($title, $post_object, $field, $post_id) {
    	$lname = get_field('last_name', $post_id);
    	$fname = get_field('first_name', $$post_id);
    	// instead of adding, replace it
    	$result = $lname.', '.$fname;
    	return $result;
    }
    
    
  • John,

    Again thank you. Although I had issues when I tried your function it led me to a solution that worked. I included my code below. Your code resulted in blank fields. It seems, although i am not sure why, that I had to use a parameter of “$object” and call “$object->ID” for each custom field. ~ Thanks again 😉

    add_filter('acf/fields/relationship/result/key=field_565de210b1c8c', 'my_relationship_result', 10, 4);
    
    function my_relationship_result( $result, $object, $field, $post ) {
    
    	// load a custom field from this $object and show it in the $result
    		$lname = get_field('last_name', $object->ID);
    		$fname = get_field('first_name', $object->ID);
                    $result =  $fname.' '.$lname;  
    		return $result;
    	}
  • glad you got it working. the double $$ at the one place may have caused an issue, it was probably causing an error in the AJAX request that was causing the blank fields.

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Relationship Field Filter by \'last_name\'’ is closed to new replies.