Support

Account

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

  • 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;
    }
    
    	
    ?>