• I’d like to have a button to reset all the custom fields for the prices, the button should operate from the author (agency) edit page because I’d like to clean the prices of all the apartments (posts) of the same author (agency) in “one shot”. I already have this button

    <button type="button" ID="target">Cancella prezzi</button>

    and when I press on it a pop up message asks me “Do you really want to delate prices?”

    Now I have to do everything that’s missing. I got an idea. I noticed that when I edited a post using the quick edit link from the post table, all the data contained in the custom fields of the posts disappeared. In practice it is as if I saved a post without custom fields. Couldn’t I use the same principle to reset a certain number of specific fields? That is, instead of telling the system: “delete the content of some custom fields”, I say “save all posts by a certain author, except for some custom fields”

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator James Huff

    (@macmanx)

    What plugin are you using for the custom fields?

    Thread Starter sacconi

    (@sacconi)

    I dont have any plug-in, it’s all coding

    Sarthak Nagoshe

    (@sarthaknagoshe2002)

    Hey, I spend a good time to write a code for your requirement.

    Step 1: Add JavaScript

    Add the following JavaScript to your admin area. You can enqueue it using admin_enqueue_scripts.

    jQuery(document).ready(function($) {
        $('#target').on('click', function() {
            if (confirm("Do you really want to delete prices?")) {
                $.ajax({
                    url: ajaxurl, // WordPress AJAX URL
                    type: 'POST',
                    data: {
                        action: 'reset_price_fields', // Custom action to be processed in PHP
                        author_id: $(this).data('author-id') // Pass the author ID
                    },
                    success: function(response) {
                        alert(response.message); // Show success message
                    },
                    error: function() {
                        alert('Error resetting prices.'); // Show error message
                    }
                });
            }
        });
    });

    Step 2: Add PHP Function

    Add this function to your theme’s functions.php file or a custom plugin.

    add_action('wp_ajax_reset_price_fields', 'reset_price_fields');
    
    function reset_price_fields() {
        // 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' => 'apartment', // 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 = ['price_field_1', 'price_field_2']; // 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;
    }

    Step 3: HTML

    Make sure to include the data-author-id attribute in your button to pass the author’s ID.

    <button type="button" id="target" data-author-id="<?php echo get_current_user_id(); ?>">Cancella prezzi</button>

    Let me know if this works for you & if requires any further modification.

    Thread Starter sacconi

    (@sacconi)

    Thanks for your effort, When you say “Add the following JavaScript to your admin area”, do you mean that I have to put the .js file in the wp-admin folder? Or do I put it in the “.js” folder inside my theme? In this same “.js” folder is currently the file “areyousure.js” that controls the pop up with the warning message

    Thread Starter sacconi

    (@sacconi)

    I have some problems. I created a .js file: “cancella_prezzi_2.js” for your step 1: I added at the end of your code:

    add_action( 'admin_enqueue_scripts', 'reset_price_fields' );

    is it correct? then I put the file into wp-admin/js , I’m doing correctly? I also did step 2 and step 3, changing the meta fields names (to clear), but at the moment nothing happens when clicking on the button…(I see the button in my author edit page)…

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.