Support

Account

Home Forums General Issues Ordering by custom field of relationship field object Reply To: Ordering by custom field of relationship field object

  • I see this is related to my current problem….

    How would I add orderby ASC to this block of code?

    add_filter('acf/fields/relationship/result/name=jam_related_home_listing', '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;
    }

    I DID FIND THE FOLLOWING ON THE CODEX AT WORDPRESS but I’m not sure which part to include in my above filter and where. https://developer.wordpress.org/reference/hooks/posts_orderby/

    // Add the callback to the posts_orderby filter
    add_filter('posts_orderby', 'orderby_pages_callback', 10, 2);
    
    // 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("post_type") === "page") {
    		# In this trivial example add a reverse menu order sort
    		return "wp_posts.menu_order DESC";
    	} else {
    		# Use provided statement instead 
    		return $orderby_statement;
    	}
    }

    I’m assuming I’d use this part:

    return "wp_posts.menu_order DESC";
    	} else {
    		# Use provided statement instead 
    		return $orderby_statement;
    	}

    LIKE THIS:

    add_filter('acf/fields/relationship/result/name=jam_related_home_listing', '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') {
    return "wp_posts.menu_order ASC";
    	} 	
            $text .= ' •  ' . $community->post_title .  ' •  Lot:  ' . $lot . '  •  SqFt:  ' . $sqft . ' •  MLS: ' . $mls . ' •  Price:  ' . $price . '';
        }
        return $text;
    }