0
votes

Here's the long and short:

I'm looking to add a global controller for post meta tags in Wordpress. I already have an established Wordpress custom admin page within our theme. I also already have added meta boxes to individual pages which store key value pairs in a more polished fashion. I have been able to call all key value pairs in a foreach loop so that they display in the administrative page. I'm having difficulty getting the input from these values on the admin panel page to properly update when the submit_button(); is clicked. I've gotten the update_post_meta function to outright erase the previous values but I have not been able to get the update_post_meta function to properly pass the $_POST value into the value at the database.

I know that the following code is not working as it's halfway through two attempts. Any help is appreciated:

 $pages = get_pages(); 
      foreach ( $pages as $page ) {
        $post_id = $page->ID;
        $meta = get_post_meta($post_id);
        /* $value1 = get_post_meta( $post_id, 'value1', true );
        $value2 = get_post_meta( $post_id, 'value2', true ); */

        echo '<h3>' . $page->post_title . '</h3>';
        echo '<label for="options_value1_' . $post_id . '">Value 1:</label>';
        echo '<input type="text" id="options_value1_' . $post_id . '" name="options_value1" value="' . $meta['value1'][0] . '" size="25" /><br />';
        echo '<label for="options_value2_' . $post_id . '">Value 2:</label>';
        echo '<input type="text" id="options_value2_' . $post_id . '" name="options_value2" value="' . $meta['value2'][0] . '" size="25" /><hr />';

        }

        foreach ( $pages as $page ) {
            update_post_meta($post_id, 'value1', $_POST('value1');
            update_post_meta($post_id, 'value2', $_POST('value2');
        }
1

1 Answers

0
votes

Hope this helps: $post_id is defined in the first loop, but not in the second loop. Also you have a syntax error in the second loop. You're not closing the parenthesis of update_post_meta() and you're not using braces for $_POST. It should be $_POST[], not $_POST(). Also, the POST parameter you're looking for is options_value1 and options_value 2, because those are the names of the fields from the first loop.

Second loop should go like this:

foreach ( $pages as $page ) {
$post_id = $page->ID;
    update_post_meta($post_id, 'value1', $_POST['options_value1']);
    update_post_meta($post_id, 'value2', $_POST['options_value2']);
}

Hope it helps.