Support

Account

Home Forums ACF PRO Search custom fields not just post title in Relationship Post Object Field

Solving

Search custom fields not just post title in Relationship Post Object Field

  • How would I based on the following filter of a Relationship Post Object in ACF Pro be able to enable search on the post object field and add the ability to search by more than just the post title? I’d like to allow searching by Sq ft, Price, Community, etc. beyond just the standard post title. Let me know.

    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);
    	
        if ($post->post_type == 'home_listings') {
            $text .= ' •  Community:  ' . $community->post_title . ' •  Lot:  ' . $lot . '  •  SqFt:  ' . $sqft . ' •  MLS: ' . $mls . ' •  Price:  ' . $price . '';
        }
        return $text;
    }
  • When searching in a relationship or post object field ACF uses the standard WP search which searches the title and content.

    In order to allow the search to look at custom fields you would need to create an acf/fields/relationship/query filter in combination with other WP filters https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/

    I don’t really have the details on how to do this because you have to find a way to only modify the search when ACF is searching for the field

    Another explanation https://www.jclabs.co.uk/customize-wordpress-search-results/ Looking at this example you would add the filter hooks inside of your acf/fields/relationship/query filter. These would need to be applied to only the current query which means you’d also need to remove these filters when the query is done. There is a hook that runs after the query in WP and that is “the_posts“, you could potentially add this filter as well and remove all the other filters when this filter is run.

  • Ok thanks for the information, I will have to explore this once I get their new website done. They can at least search by the title of the posts. Not ideal, especially for real estate with Beds, Baths, MLS number, lot number etc. Lots of other data they can search within posts to find the one they want to select! I WISH for ACF to include this natively please. The question is, why not? It would make our lives easier and especially the clients lives. Allow this additional search functionality to be enabled when we create the Post Object in the ACF field group settings. The settings for this feature would show all available custom field data, including WP native ones and allow us to select which we’d like the client to search using.

  • Why not in ACF.

    The goal and scope of ACF from the beginning is to allow the creation of fields management in the admin that anyone could accomplish using built in WP functions for meta boxes (custom fields) allowing us to make this part of the development process easier and faster.

    To this end ACF uses a standard WP_Query() to get the posts that will be listed in relationship and post object fields.

    ACF also supplies a plethora of hooks for filters and actions that allow extending it for those where the functionality that 90% of users is not enough for other 10% of us.

    Beyond this I can only suggest contacting the developers.

  • QUICK QUESTION….(hopefully)

    How would I add the following orderby ASC to the post Object Filter function I shared above in this thread? I’m just learning PHP so it would be really helpful if you could share how I’d integrate this so that the additional data I’m showing in the backend in the Post Object sorts the list alphabetically starting A-Z:

    I’ve used this line in a WP_Query for a different code block but I’m unsure how to implement into the post object relationship filter you helped me create.

    'orderby'=> 'title', 'order' => 'ASC'

    WOULD IT LOOK LIKE THIS »

    <?php
    	add_filter('acf/fields/relationship/result/name=jam_related_home_listing', 'my_acf_fields_relationship_result', 'posts_orderby', 'orderby_pages_callback', 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 . '';
        }
    	
    // The posts_orderby filter
    function orderby_pages_callback($orderby_statement, $wp_query) {
    	# Verify correct post type, or any other query variable
    	if ($wp_query->get("home_listings") === "post") {
    		# In this trivial example add a reverse menu order sort
    		return "wp_posts.menu_order ASC";
    	} else {
    		# Use provided statement instead 
    		return $orderby_statement;
    	}
    }
        return $text;
    }
    
    	
    ?>
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.