WordPress bulk post approval and publishing doesn’t work

I’m new to WordPress plugin development. I’m trying to develop a simple plugin that creates a sub-menu named Approval under Posts menu and checks if any draft post has the post meta approve set to value 'pre-publish'.

The plugin works fine up to this point.

Read More

There’s a button in the Approval sub-menu page to bulk approve all the posts with post meta approve set to the value 'pre-publish'. The bulk approve process is supposed to set the post meta approve to 'approved' and then publish all those posts.

However, it doesn’t work as I expected. Somehow the posts disappear instead!

This is my plugin CODE:

<?php
/*
Plugin Name: Approve Posts
Description: Bulk approve posts.
*/

// Create sub-menu under Posts
function karma_posts_approval_menu() {
    add_submenu_page( 'edit.php', 'Approval Posts', 'Approval', 'manage_options',
                      'approval-posts', 'karma_posts_approval_page' );
}
add_action( 'admin_menu', 'karma_posts_approval_menu' );

// Approval sub-menu page content
function karma_posts_approval_page() {
    $posts = karma_get_pre_publish_posts();
    // Check for approve button submit
    if ( isset( $_POST['approve_all_posts'] ) ) {
        karma_approve_all_posts( $posts );
    }
    else {
        karma_list_pre_publish_posts( $posts );
    }
}

function karma_approve_all_posts( $posts ) {
    if( count( $posts ) > 0 ) {
        foreach ( $posts as $post ) {
            update_post_meta( $post->ID, 'approve', 'approved' );
            $update_post = array(
                'ID' => $post->ID,
                'post_status' => 'published'
            );
            wp_update_post( $update_post  );
        }
    }
    ?>
    <div id="message" class="updated notice is-dismissible">
        <p>All posts have been approved and published.</p>
    </div>
    <?php
}

function karma_list_pre_publish_posts( $posts ) {
    if( count( $posts ) > 0 ) {
        echo '<h1>Posts requiring approval:</h1>';
        echo '<ul>';
        foreach ( $posts as $post ) {
            echo '<li><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></li>';
        }
        echo '</ul>';
        ?>
        <form method="post">
            <input type="submit" name="approve_all_posts" value="Approve" class="button button-primary">
        </form>
        <?php
    }
    else {
        echo "<h1>Nothing to approve.</h1>";
    }
}

function karma_get_pre_publish_posts() {
    // get all draft posts with 'approve' custom field set to 'pre-publish'
    $args = array(
        'post_type' => 'post',
        'post_status' => 'draft'
    );
    $approval_posts = array();
    $draft_posts = get_posts( $args );
    foreach ( $draft_posts as $post ) {
        if( get_post_meta( $post->ID, 'approve', true ) == 'pre-publish' ) {
            $approval_posts[] = $post;
        }
    }
    return $approval_posts;
}

Related posts

Leave a Reply

1 comment

  1. The problem I can immediately see is, you’ve set:

    'post_status' => 'published'
    

    This should be:

    'post_status' => 'publish'
    

    Also, since you are only updating the post status, it’s less error prone and more appropriate to use wp_publish_post function instead of wp_update_post function.

    With this change, your karma_approve_all_posts() function will look like this:

    function karma_approve_all_posts( $posts ) {
        if( count( $posts ) > 0 ) {
            foreach ( $posts as $post ) {
                update_post_meta( $post->ID, 'approve', 'approved' );
                wp_publish_post( $post->ID );
            }
        }
        ?>
        <div id="message" class="updated notice is-dismissible">
            <p>All posts have been approved and published.</p>
        </div>
        <?php
    }