function cp_delete_ad_listing($postid) { global $wpdb; $attachments_query = $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_type='attachment'", $postid); $attachments = $wpdb->get_results($attachments_query); // delete all associated attachments if ($attachments) { foreach ($attachments as $attachment) { wp_delete_attachment($attachment->ID, true); } } // delete geo location cp_delete_geocode($postid); // delete post and it's revisions, comments, meta if (wp_delete_post($postid, true)) { return true; } else { return false; } }
function cp_do_update_geocode($meta_id, $post_id, $meta_key, $meta_value) { global $wpdb, $cp_options; if (!in_array($meta_key, array('cp_city', 'cp_country', 'cp_state', 'cp_street', 'cp_zipcode'))) { return; } // remove old geocode result cp_delete_geocode($post_id); $address = ''; $fields = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key IN ('cp_street','cp_city','cp_state','cp_zipcode','cp_country')", $post_id), OBJECT_K); foreach ($fields as $field) { if (!empty($field->meta_value)) { $address .= $field->meta_value . ', '; } } $address = rtrim($address, ', '); if (empty($address)) { return; } $region = $cp_options->gmaps_region; $address = urlencode($address); $geocode = json_decode(wp_remote_retrieve_body(wp_remote_get("http://maps.googleapis.com/maps/api/geocode/json?address={$address}&sensor=false®ion={$region}"))); if ('OK' == $geocode->status) { cp_update_geocode($post_id, '', $geocode->results[0]->geometry->location->lat, $geocode->results[0]->geometry->location->lng); } }
/** * Deletes all meta and attachments related with the ad. * * @since 3.5 * * @param int $post_id * * @return void */ function cp_delete_ad_meta($post_id) { global $wpdb; if (APP_POST_TYPE != get_post_type($post_id)) { return; } $attachments_query = $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_type='attachment'", $post_id); $attachments = $wpdb->get_results($attachments_query); // delete all associated attachments if ($attachments) { foreach ($attachments as $attachment) { wp_delete_attachment($attachment->ID, true); } } // delete geo location cp_delete_geocode($post_id); }