-
-
Save hazratbilal0079/ba6e7932d37b7654c02a503863956a28 to your computer and use it in GitHub Desktop.
Delete Multiple Tickets Based on Specified Criteria from a specific user or Delete All tickets of a user From Frontend in WordPress using hook or Code Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Hook Name | |
deleteownerdata | |
--PHP Hook | |
add_action('jet-form-builder/custom-action/deleteownerdata', function($request, $action_handler) { | |
// Sanitize the input fields to prevent any security issues | |
$property_name = isset($_POST['select_property']) ? sanitize_text_field($_POST['select_property']) : ''; | |
$community_name = isset($_POST['community-name']) ? sanitize_text_field($_POST['community-name']) : ''; | |
$responsible_person = sanitize_text_field($_POST['responsible_person']); | |
// Validate that responsible_person is a number (user ID) | |
if (!is_numeric($responsible_person)) { | |
// If not a number, handle the error appropriately (log, return, etc.) | |
error_log('Invalid responsible_person value: must be a numeric user ID.'); | |
return; | |
} | |
// Convert responsible_person to an integer | |
$responsible_person = (int)$responsible_person; | |
// Use global $wpdb to access the WordPress database | |
global $wpdb; | |
// Initialize an empty array to store post IDs | |
$post_ids = array(); | |
// Prepare base SQL query | |
$base_query = "DELETE FROM {$wpdb->prefix}posts WHERE post_type = 'owner-data'"; | |
// Check if property name has value | |
if (!empty($property_name)) { | |
// Add condition to delete posts with the given property name | |
$base_query .= $wpdb->prepare( | |
" AND ID IN ( | |
SELECT post_id | |
FROM {$wpdb->prefix}postmeta | |
WHERE meta_key = 'property-name' AND meta_value = %s | |
)", | |
$property_name | |
); | |
} | |
// Check if community name has value | |
if (!empty($community_name)) { | |
// Add condition to delete posts with the given community name | |
$base_query .= $wpdb->prepare( | |
" AND ID IN ( | |
SELECT post_id | |
FROM {$wpdb->prefix}postmeta | |
WHERE meta_key = 'community-name' AND meta_value = %s | |
)", | |
$community_name | |
); | |
} | |
// Add condition to delete posts with the given responsible person | |
$base_query .= $wpdb->prepare( | |
" AND ID IN ( | |
SELECT post_id | |
FROM {$wpdb->prefix}postmeta | |
WHERE meta_key = 'responsible-person' AND meta_value = %d | |
)", | |
$responsible_person | |
); | |
// Execute the delete query | |
$wpdb->query($base_query); | |
}, 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment