Function Reference: geodir_location_delete_by_id

Summary

Remove location and its data using location ID.

Global Values

$wpdb
(object) (required) WordPress Database object.

Default: None
$plugin_prefix
(string) (required) Geodirectory plugin table prefix.

Default: None

Package

GeoDirectory_Location_Manager

Parameters

$id
(int) (required) Location ID.

Default: None

Return Values

(bool)
  • Returns true when successful deletion.

Change Log

Since: 1.0.0

Actions

‘geodir_location_before_delete’ [Line: 2793]

‘geodir_location_after_delete’ [Line: 2824]

Source File

geodir_location_delete_by_id() is located in geodir_location_manager/geodir_location_functions.php [Line: 2784]

Source Code

function geodir_location_delete_by_id( $id ) {
	global $wpdb, $plugin_prefix;
	
	if ( !current_user_can( 'manage_options' ) || !$id > 0 ) {
		return false;
	}

	$geodir_posttypes = geodir_get_posttypes();
	
	do_action( 'geodir_location_before_delete', $id );
	
	$location_info = $wpdb->get_row( $wpdb->prepare( "SELECT city_slug, is_default FROM " . POST_LOCATION_TABLE . " WHERE location_id = %d", array( $id ) ) );
	if ( !empty( $location_info ) && !empty( $location_info->is_default ) ) {
		return false; // Default location
	}
	
	foreach( $geodir_posttypes as $geodir_posttype ) {
		
		$table = $plugin_prefix . $geodir_posttype . '_detail';
		
		$rows = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM " . $table . " WHERE post_location_id = %d", array( $id ) ) );
		
		if ( !empty( $rows ) ) {
			foreach ( $rows as $row ) {
				wp_delete_post( $row->post_id ); // Delete post
			}
		}
	}
	
	// Remove neighbourhood location
	$wpdb->query( $wpdb->prepare( "DELETE FROM " . POST_NEIGHBOURHOOD_TABLE . " WHERE hood_location_id = %d", array( $id ) ) );
			
	// Remove current location data
	if ( !empty( $location_info ) && !empty( $location_info->city_slug ) && isset( $_SESSION['gd_city'] ) && $_SESSION['gd_city'] == $location_info->city_slug ) {
		unset(	$_SESSION['gd_multi_location'], $_SESSION['gd_city'], $_SESSION['gd_region'], $_SESSION['gd_country'] );
	}
	
	// Remove post location data
	$wpdb->query( $wpdb->prepare( "DELETE FROM " . POST_LOCATION_TABLE . " WHERE location_id = %d", array( $id ) ) );
	
	do_action( 'geodir_location_after_delete', $id );
	
	return true;
}