• Hi.

    I would like to know if there is something like or similar to this in WordPress.

    The point is, I want my WordPress posts to have more than one owner or author, you could say.

    But my website would function as home service providers.
    So what I would like is that automatically when a person fills for example “a form” in said post. Automatically detect which of all the owners of the post has the closest address in our database.

    Is there any way I can do this?

Viewing 1 replies (of 1 total)
  • Hi @aleperix. The easiest way I can think of to do something like this would be to install a plugin like Advanced Custom Fields.

    First, set up a ‘Owners’ field with ‘Type = User’ and ‘Select multiple values? = Yes’. Show this when ‘Post type = post’.

    Then, set up a ‘Location’ field with Type = Google Map. Show this when ‘User form’ = ‘Add / Edit’.

    Now write some custom code in the post template that loops through all of the post’s owners and selects the one that is nearest to a given address.

    $origin = array( 'lat' => -33.865143, 'lng' => 151.209900 );
    $owners = get_field( 'owners' );
    foreach ( $owners as $owner ) {
    	$location = get_field( 'location', 'user_' . $owner->ID );
    	$distance = haversineGreatCircleDistance( $origin['lat'], $origin['lng'], $location['lat'], $location['lng'] );
    	if ( ! isset( $nearest_distance ) || $distance < $nearest_distance ) {
    		$nearest_distance = $distance;
    		$nearest_owner = $owner;
    	}
    }
    
    // From https://stackoverflow.com/a/10054282
    function haversineGreatCircleDistance(
      $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
    {
      // convert from degrees to radians
      $latFrom = deg2rad($latitudeFrom);
      $lonFrom = deg2rad($longitudeFrom);
      $latTo = deg2rad($latitudeTo);
      $lonTo = deg2rad($longitudeTo);
    
      $latDelta = $latTo - $latFrom;
      $lonDelta = $lonTo - $lonFrom;
    
      $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
        cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
      return $angle * $earthRadius;
    }
    
    printf( 'The nearest owner is: %s', $nearest_owner->display_name );
Viewing 1 replies (of 1 total)
  • The topic ‘Co-Author Automatic Detect Nearby Author’ is closed to new replies.