• Hello, I created a Relational Post Object Filter so that I could show more information about a post in the selection via the admin. The post object is shared due to the custom field names for the relationship fields being the exact same. This is presenting me with a problem where it’s showing empty post object information on the other side of the relationship.

    For example:
    Home Listings – Shows a relationship post object field to select a community to associate the home listing.
    Communities – Shows a relationship post object field to select a home listing to associate with the community.

    On the Communities post in the admin, the post object selection is showing unwanted filtered data.

    How do I exclude the unwanted data from the filter on the the communities posts in the admin when they both share the same custom field relationship name in ACF Pro settings?

    Here’s my code for the filter that works great on one-side of the relationship but I’m not sure how to get this to work so the Community select doesn’t show Home Listing Filtered info:

    add_filter('acf/fields/relationship/result/name=show_as_community_model', function($title, $post, $field, $post_id) {
      
      $lot = get_field('lot', $post->ID);
      $sqft = get_field('sq_ft', $post->ID);
      $mls = get_field('mls', $post->ID);
      $price = get_field('price',$post->ID);
    	
      $title .= ' ?  Lot:  ' . $lot . '  ?  SqFt:  ' . $sqft . ' ?  MLS: ' . $mls . ' ?  Price:  ' . $price . '';
    	
    	return $title;
    }, 10, 4);

    `

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author John Huebner

    (@hube2)

    You need to check the post type of for where the field is being shown, using $post (second argument)

    For example at the top of your filter

    
    if ($post->post_type == 'post-type-1') {
      // set value for this post type
    } else {
      // set title for other post type
    }
    
    
    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    Thank you for the help! Would this be correct syntax???…

    add_filter('acf/fields/relationship/result/name=show_as_community_model', function($title, $post, $field, $post_id) {
      if ($post->post_type == 'home_listing') {
    
    $lot = get_field('lot', $post->ID);
      $sqft = get_field('sq_ft', $post->ID);
      $mls = get_field('mls', $post->ID);
      $price = get_field('price',$post->ID);
    
      $title .= ' ?  Lot:  ' . $lot . '  ?  SqFt:  ' . $sqft . ' ?  MLS: ' . $mls . ' ?  Price:  ' . $price . '';
    	
    	return $title;
    }, 10, 4);
    
    } else {
    
    $title .= '';
    
    }
    Plugin Author John Huebner

    (@hube2)

    looks right to me

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    I just pasted it into functions and something is broken with the syntax?

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    What does the 10 and 4 code do? I think my syntax organization is off somehow but I’m not sure what I did wrong ??

    }, 10, 4);

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    @hube2 I came up with code that worked see below but the $community variable I call in the following script is a Post Object. For some reason it makes the entire custom field in the admin empty and doesn’t shot anything. the second I remove the $community post object custom field code from the following block it displays just fine. Do you know how to display the title of a Post Object in a filter like this? I just want to include the community name in the filter along with those other fields but it refuses to work because $community is a post object ACF field and I don’t know how to write the code so that the community title shows up along with the rest of the fields (which are all standard ACF text fields)…..

    add_filter('acf/fields/relationship/result/name=show_as_community_model', 'my_acf_fields_relationship_result', 10, 4);
    function my_acf_fields_relationship_result( $text, $post, $field, $post_id ) {
    	
      $community = get_field ('community', $post->ID);
      $sqft = get_field('sq_ft', $post->ID);
      $mls = get_field('mls', $post->ID);
      $price = get_field('price',$post->ID);
    	
        if ($post->post_type == 'home_listings') {
            $text .= '  ?  ' . $community . '  ?  Lot:  ' . $lot . '  ?  SqFt:  ' . $sqft . ' ?  MLS: ' . $mls . ' ?  Price:  ' . $price . '';
        }
        return $text;
    }
    Plugin Author John Huebner

    (@hube2)

    
    }, 10, 4);
    

    Priority, Number of args passed

    Plugin Author John Huebner

    (@hube2)

    if the field “community” is a post object (an object of the class WP_Post). This is causing an error when you try to concatenate it into the string, likely causing an “to string” conversion error.

    If you want the title of the post then you should be using $community->post_title

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    Still won’t work…I adding post_title…..can you tell me where I’m not typing it correctly?

    add_filter('acf/fields/relationship/result/name=show_as_community_model', 'my_acf_fields_relationship_result', 10, 4);
    function my_acf_fields_relationship_result( $text, $post, $field, $post_id ) {
      $community = get_field('community', $community->post_title);
      $sqft = get_field('sq_ft', $post->ID);
      $mls = get_field('mls', $post->ID);
      $price = get_field('price',$post->ID);
    	
        if ($post->post_type == 'home_listings') {
            $text .= '  ?  ' . $community . '  ?  Lot:  ' . $lot . '  ?  SqFt:  ' . $sqft . ' ?  MLS: ' . $mls . ' ?  Price:  ' . $price . '';
        }
        return $text;
    }
    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    @hube2 I figured this out and will mark it as resolved!!!!! Here’s the final working code for anyone else who might want this same functionality. You’re welcome ya’ll! AND big thank you to Mr. John Huebner for your awesome help!

    add_filter('acf/fields/relationship/result/name=show_as_community_model', 'my_acf_fields_relationship_result', 10, 5);
    function my_acf_fields_relationship_result( $text, $post, $field, $post_id ) {
    
      $community = get_field ('community', $post->ID);
      $sqft = get_field('sq_ft', $post->ID);
      $mls = get_field('mls', $post->ID);
      $price = get_field('price',$post->ID);
      $lot = get_field('lot', $post->ID);
    	
        if ($post->post_type == 'home_listings') {
    		
            $text .= ' ?  ' . $community->post_title .  ' ?  Lot:  ' . $lot . '  ?  SqFt:  ' . $sqft . ' ?  MLS: ' . $mls . ' ?  Price:  ' . $price . '';
        }
        return $text;
    }
    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    @hube2 Would you be able to help me enable searching by more than the post title? I’d love to allow the user to be able to search the show_as_community_model post object field by more than the home listing post title but rather by the Lot, price, mls, sq ft and community. I’m sure there is a filter out there somewhere but I’m new to this and it would make browsing for a home listing to associate as a model home so much quicker and easier for the user.

    Plugin Author John Huebner

    (@hube2)

    these questions are really not about the use of this plugin. If you need help with ACF then you should be using the ACF forum.

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    Gotcha, I’ll go ahead and submit my question about the search aspect of the post object on the ACF forum. Thank you again.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Relationship Post Object Filtering Issue’ is closed to new replies.