Sacconi69

step 1 + step 2

Nov 1st, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. // CANCELLA PREZZI 2- STEP 1
  2.  
  3. function reset_price_fields( $hook ) {
  4. if ( 'edit.php' != $hook ) {
  5. return;
  6. }
  7. wp_enqueue_script( 'ajax-script', get_template_directory_uri . '/js/cancella_prezzi_2.js/', array('jquery'), '1.0' );
  8. $title_nonce = wp_create_nonce( 'title_example' );
  9. wp_localize_script(
  10. 'ajax-script',
  11. 'my_ajax_obj',
  12. array(
  13. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  14. 'nonce' => $title_nonce,
  15. )
  16. );
  17.  
  18.  
  19. }
  20.  
  21. // CANCELLA PREZZI 2- STEP 2
  22.  
  23. add_action('wp_ajax_reset_price_fields', 'reset_price_fields2');
  24.  
  25. check_ajax_referer( 'title_example' );
  26.  
  27. function reset_price_fields2() {
  28. // Check for the required parameter
  29. if (!isset($_POST['author_id'])) {
  30. wp_send_json_error(['message' => 'No author ID provided.']);
  31. exit;
  32. }
  33.  
  34. $author_id = intval($_POST['author_id']);
  35. $args = [
  36. 'post_type' => 'post', // Change this to your custom post type
  37. 'author' => $author_id,
  38. 'posts_per_page' => -1 // Get all posts
  39. ];
  40.  
  41. $query = new WP_Query($args);
  42.  
  43. if ($query->have_posts()) {
  44. while ($query->have_posts()) {
  45. $query->the_post();
  46. $post_id = get_the_ID();
  47.  
  48. // Get current custom fields
  49. $custom_fields = get_post_meta($post_id);
  50.  
  51. // Prepare an array of custom fields to reset
  52. $fields_to_reset = ['1_per_hol', '2_periodo']; // Replace with your custom field keys
  53.  
  54. foreach ($fields_to_reset as $field) {
  55. if (array_key_exists($field, $custom_fields)) {
  56. // Delete the specific custom field
  57. delete_post_meta($post_id, $field);
  58. }
  59. }
  60. }
  61. wp_send_json_success(['message' => 'Prices have been reset successfully.']);
  62. } else {
  63. wp_send_json_error(['message' => 'No posts found for this author.']);
  64. }
  65.  
  66. wp_reset_postdata();
  67. exit;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment