Thanks in advance to anyone and everyone who is taking the time to respond. So here is what I am trying to achieve: Essentially I am trying to create a plugin to just edit all the meta descriptions on my pages from a plugin admin page.

I am doing this by using the following logic - print all the metas from the database on a page. Using jQuery AJAX get all of the page ids and values from said page. On submit - push all the updated values to their respective spots in the database and update those meta descriptions.

I am not sure if I have flawed code on a conceptual level or if there is just something basic I am missing but below is the code that I have written. I have tried reading and testing just about everything I can and no avail. Any enlightenment would be greatly apprecaite.

Please note - my callback function has three foreach loops - none of which work but I left them so that you can see a few things that I have tried.

Thanks again!

<!-- Page Creation -->
function ews_meta_page_creation_stephen() { ?>
    <form class="metas-plugin" method="post">
        <?php display_ews_metas_ids(); ?>
        <?php submit_button();?>

<?php } ?>

<!-- jQuery & AJAX Function -->
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
    jQuery(document).ready(function() {
        // On form submission - run the function
        jQuery("input#submit").click(function(event) {
            var postMeta = [];
            var postID = [];
            // For each input - push the values to their respective empty arrays
            jQuery(".descinput").each(function() {
            // Once completed - json-ify the values for posting to PHP
            var finalPostMeta = JSON.stringify(postMeta);
            var finalPostID = JSON.stringify(postID);
            // Send the arrays using POST in an AJAX Request
                type: "POST",
                url: 'https://dev.ewsproduction.com/dev6/ewstheme/wp-admin/admin-ajax.php',
                data: {
                    action: "update_meta_descriptions",
                    post_ids: finalPostID,
                    metas: finalPostMeta,
                // Just for testing to make sure the values that are being sent are correct
                success: function( data ) {

<!-- Callback AJAX function used to update the respective meta descriptions -->
add_action( 'wp_ajax_update_meta_descriptions', 'update_meta_descriptions' );
    // AJAX Callback function to process the POST Data
    function update_meta_descriptions() {
        // Should Return Meta Description Array in a JSON Format
        $metas = json_decode(stripslashes($_POST['finalPostMeta']));
        // Should Return Post ID Array in a JSON Format
        $post_ids = json_decode(stripslashes($_POST['finalPostID']));

            // None of the below for each loops worked. What I am trying to achieve is using the post id - update the post meta for the respective post with whatever value was added before submit. 

            // Did not work but an example of what I tried
            foreach ($post_ids as $key=> $meta) {
                update_post_meta( $post_id, '_metadescs', $meta);

            // Did not work but an example of what I tried
            foreach(array_combine($post_ids, $metas) as $post_ids => $metas) {
                update_post_meta( $post_id, '_metadescs', $meta);

            // Did not work but an example of what I tried
            foreach ($post_ids as $key=> $meta) {
                update_post_meta( $post_id, '_metadescs', $meta);


1 Answer 1


$post_id is never defined in any of your update loops, and this shows that a simple single loop is not enough to achive this.

Since you are looping two concurrently created arrays, you would need to do something like this instead, using the index to match values:

for ( $i = 0; $i < count($post_ids); $i++ ) {
    update_post_meta( $post_ids[$i], '_metadescs', $metas[$i] );

Alternatively you could do a loop within a loop and match the index this way:

foreach ( $post_ids as $i => $post_id ) {
    foreach ( $metas as $j => $meta ) {
        if ($i == $j) {update_post_meta( $post_id, '_metadescs', $meta );}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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