0

Need your help. I have created a custom post type and have a list table of the latest 10 posts. I need to change the post status of those post which I check a checkbox. However, I can not figure out how to get post ID and pass them into wp_update_post.

<div class="card w-100">
  <form action="" method="post">
    <input type="hidden" name="test" value="<?php echo wp_create_nonce('test'); ?>"/>
    <input type="submit" value="<?php _e('Update'); ?>"/>
  </form>
  <table class="table table-striped w-100">
    <thead>
      <tr>
        <th>ID</th>
        <th>Nest</th>
        <th>Panel Number</th>
        <th>Steel Type</th>
        <th>Quantity</th>
        <th>Date</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <?php

// The Query
$args = array(
        'post_type'=> 'reworkticket',
        'post_status' =>  'draft',
        'posts_per_page' => 10,
        'order'    => 'DESC'
            );
query_posts($args);

// The Loop
while (have_posts()) : the_post();
global $post; ?>
                <tr>
                  <td><?php echo $post->ID; ?></td>
                  <td><?php echo get_post_meta($post->ID, '_rework_ticket_nest_number', true); ?></td>
                  <td><?php echo get_post_meta($post->ID, '_rework_ticket_panel_number', true); ?></td>
                  <td><?php echo get_post_meta($post->ID, '_rework_ticket_panel_steel', true); ?></td>
                  <td><?php echo get_post_meta($post->ID, '_rework_ticket_panel_quantity', true); ?></td>
                  <td><?php echo get_the_date('jS, M, Y. H:i'); ?></td>
                  <td>
                    <form action="" method="post">
                      <input type="checkbox" name="rwk_chk[]" value="<?php echo get_the_id(); ?>" />
                    </form>
                  </td>
                </tr>
            <?php

if (isset($_POST['test']) && wp_verify_nonce($_POST['test'], 'test')) {
    $a = implode(',', $_POST['rwk_chk']);

    $my_post = array(
                'ID'            => $a, //How to pass ID here?
                'post_status'   =>  'publish',
              );
    // Update the post into the database
    $update_ticket = wp_update_post($my_post);
}

endwhile;
// Reset Query
wp_reset_query(); ?>
            </tbody>
        </table>
    </div>
  • wp_update_post function only update the single id , to do it for many rows in db . We can loop the Id in foreach or use sql function like this($ids = array(); $wpdb->query("UPDATE {$wpdb->posts} SET post_status = 'publish' WHERE ID IN (".implode(',',$ids)).")");) – Gopala krishnan Oct 3 '19 at 8:24

Your Answer

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

Browse other questions tagged or ask your own question.