Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CANCELLA PREZZI 2- STEP 1
- function reset_price_fields( $hook ) {
- if ( 'edit.php' != $hook ) {
- return;
- }
- wp_enqueue_script( 'ajax-script', get_template_directory_uri . '/js/cancella_prezzi_2.js/', array('jquery'), '1.0' );
- $title_nonce = wp_create_nonce( 'title_example' );
- wp_localize_script(
- 'ajax-script',
- 'my_ajax_obj',
- array(
- 'ajax_url' => admin_url( 'admin-ajax.php' ),
- 'nonce' => $title_nonce,
- )
- );
- }
- // CANCELLA PREZZI 2- STEP 2
- add_action('wp_ajax_reset_price_fields', 'reset_price_fields2');
- check_ajax_referer( 'title_example' );
- function reset_price_fields2() {
- // Check for the required parameter
- if (!isset($_POST['author_id'])) {
- wp_send_json_error(['message' => 'No author ID provided.']);
- exit;
- }
- $author_id = intval($_POST['author_id']);
- $args = [
- 'post_type' => 'post', // Change this to your custom post type
- 'author' => $author_id,
- 'posts_per_page' => -1 // Get all posts
- ];
- $query = new WP_Query($args);
- if ($query->have_posts()) {
- while ($query->have_posts()) {
- $query->the_post();
- $post_id = get_the_ID();
- // Get current custom fields
- $custom_fields = get_post_meta($post_id);
- // Prepare an array of custom fields to reset
- $fields_to_reset = ['1_per_hol', '2_periodo']; // Replace with your custom field keys
- foreach ($fields_to_reset as $field) {
- if (array_key_exists($field, $custom_fields)) {
- // Delete the specific custom field
- delete_post_meta($post_id, $field);
- }
- }
- }
- wp_send_json_success(['message' => 'Prices have been reset successfully.']);
- } else {
- wp_send_json_error(['message' => 'No posts found for this author.']);
- }
- wp_reset_postdata();
- exit;
- }
Advertisement
Add Comment
Please, Sign In to add comment