I need some help on making a batch post meta updater I'm trying to add to my options/setting page.

Basically I'm looking for a way to click a button a batch runs through all post of a selected CPT "listings" and updates a meta field as well as run a function.

Does my code look right? And how do I add a button that wont refresh the page, but will start this function.

function batch_update_meta(){

        $post_type = "listings";
        $post_type_object = get_post_type_object($post_type);
        $label = $post_type_object->label;
        echo  All . "$label";
        $posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));

        foreach ($posts as $post) {

            $meta_value1 = get_post_meta($post->ID, $key1, True);

            if (!empty($meta_value1)){

            $meta_value2 = media_process($meta_value1, $post->ID); //Returns a string after it finishes process.
            update_post_meta($post->ID, $key2, $meta_value2)
            echo $post->post_title." UPDATED"; //Prints updated after ran.



1 Answer 1


Take a look at the AJAX documentation, there are plenty of examples on this site and across the Googles to trigger a server event without refreshing the page.

In this case, WP is listening to an AJAX call that looks like http://example.com/wp-admin/admin-ajax.php?action=wpse20160318_batch_update. When triggered, it'll run through your batch process an output a JSON message that can be consumed by JavaScript after the call is completed.

The major modifications are just adding an AJAX listener to trigger the batch_update_meta, removing any echos, and returning JSON when complete.

function batch_update_meta() {

    $post_type        = "listings";
    $post_type_object = get_post_type_object( $post_type );
    $label            = $post_type_object->label;
    $processed = array ();

    $posts = get_posts( array (
        'post_type'        => $post_type,
        'post_status'      => 'publish',
        'suppress_filters' => false,
        'posts_per_page'   => - 1,
    ) );

    foreach ( $posts as $post ) {

        $key1 = 'my_meta_key_1';
        $key2 = 'my_meta_key_2';

        $meta_value1 = get_post_meta( $post->ID, $key1, true );

        if ( ! empty( $meta_value1 ) ) {

            // Returns a string after it finishes process.
            $meta_value2 = media_process( $meta_value1, $post->ID );

            // Update the meta
            update_post_meta( $post->ID, $key2, $meta_value2, $meta_value1 );

            // Add to our success list
            $processed[] = $post->post_title;
        else {

    // give a response for AJAX
    wp_send_json_success( array (
        'label'     => $label,
        'processed' => $processed,
        'message'   => 'Batch Completed.',
    ) );

// callback for AJAX function
function wpse20160318_batch_update_meta() {

    // run your batch update

// must be logged in to trigger - http://example.com/wp-admin/admin-ajax.php?action=wpse20160318_batch_update
add_action( 'wp_ajax_wpse20160318_batch_update', 'wpse20160318_batch_update_meta' );
  • So how will i fire this function from a button using Ajax. I know it might be simple.. just want to make sure I got it...
    – samjco
    Mar 18, 2016 at 8:10
  • jQuery.post is outlined in the documentation. Give it a try then ask for help on specific issues you're having. The 'action' in this case is the name of the callback function, everything after 'wp_ajax_'.
    – jgraup
    Mar 18, 2016 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.