0

Been having issues with getting a 500 error. Sometimes a change is saved, usually not. I've output the $key/$values to verify it's reaching the function and foreach. The output is the first key/value pair over and over...

I'm approaching this this way because I will have an unknown number of "officers" that will be output as a custom post type.

$name = 'officer' . '-' . $officer_id . '-' . $count;
    ?>
        <label for="<?php echo $name; ?>">Officer #<?php echo $count; ?></label>    
        <input type="text" name="<?php echo $name; ?>" value="<?php echo $officer_info[0]->post_title; ?>">
        <br />
    <?php
    $count++;

I tried entering officers as an array:

$name = 'officer' . '-' . $officer_id . '-' . $count;
    ?>
        <label for="['<?php echo $name; ?>']">Officer #<?php echo $count; ?></label>    
        <input type="text" name="<?php echo $name; ?>" value="<?php echo $officer_info[0]->post_title; ?>">

With the id as the key, no change.

I've tried continue if 'officer' isn't in the $key with no change. I tried 'pre_post_update' instead of 'save_post' with no change. I've added priority and number (10, 2) of variables with no change.

add_action( 'save_post', 'officers_onto_board_page_save', 10, 2 );
function officers_onto_board_page_save( $post_id ) {
    global $wpdb;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) {
        return;
    } 

    foreach ($_POST as $key => $item) {
        echo $key . ' , ' . $item;

        if (strpos($key, 'officer') == 0) {
            $parts = explode('-', $key);

            $my_post = array(
                  'ID'           => $parts[1],
                  'post_title'   => $item
              );
            wp_update_post( $my_post );
        }
    } 
}

Any suggestions/recommendations would be appreciated.

example response: _wpnonce , dc7a6eaea0_wpnonce , dc7a6eaea0_wpnonce , dc7a6eaea0_wpnonce , dc7a6eaea0_wpnonce , dc7a6eaea0_wpnonce , dc7a6eaea0_wpnonce , dc7a6eaea0_wpnonce , dc7a6eaea0 (over and over)

2
  • Is there anything useful in the server logs when the 500 error occurs? Oct 13, 2015 at 17:28
  • Aric, found an answer before checking the logs. Great suggestion!
    – momn99
    Oct 15, 2015 at 22:15

1 Answer 1

0

I found an answer.

Essentially I had an infinite loop that was quite puzzling at first. It made more sense as I saw that it never iterated beyond the first item in the post array.

I found the answer here: https://tommcfarlin.com/update-post-in-save-post-action/

The issue is that wp_update_post hooks into save_post... so every time I would reach that, it would restart again. To fix this, I changed my code to read:

function officers_onto_board_page_save( $post_id ) {
    remove_action( 'save_post', 'officers_onto_board_page_save' );


    //DO STUFF


   add_action( 'save_post', 'officers_onto_board_page_save');
}

By removing the function and then re-adding it to save_post, it operates as expected.

Your Answer

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

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