4

I am trying to list the name of the moderators (users with editor role) that publish pending posts. I created a dashboard widget that shows the latest 10 published posts and the author, but I have problems getting the user that published it. I thought the_modified_author might be it, but I'm not sure if there's a better way to tell who published the post? Either way, my method isn't working. Here's my code so far:

function dashboard_widget_function() {

    $args = array( 'post_status' => 'publish', 'numberposts' => '10', 'post_type' => 'post');

        $recent_posts = wp_get_recent_posts( $args );

     echo '<table><tr>
    <th>Post Title</th>
    <th>Author</th>
    <th>Moderated by</th></tr>';

        foreach( $recent_posts as $recent ){ 
            $post_author = get_user_by( 'id', $recent['post_author'] );
            echo '<tr><td><a href="' . get_permalink($recent["ID"]) . '" title="Read: '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a></td>';
            echo '<td>'. $post_author->display_name .'</td>';
            echo '<td>'.get_the_modified_author().'</td></tr>';

        }
        echo '</table>';
}

2 Answers 2

4

The reason that get_the_modified_author() is not working is that it relies on being used within the WordPress loop. wp_get_recent_posts() does not set up a global post object. Here is a complete example based on your original code that replaces get_the_modified_author() with some simple code to get the name of the last person who edited the post (which is essentially what get_the_modified_author() does):

/**
 * Add a widget to the dashboard.
 *
 * This function is hooked into the 'wp_dashboard_setup' action below.
 */
function example_add_dashboard_widgets() {
    wp_add_dashboard_widget(
        'example_dashboard_widget', // Widget slug.
        'Example Dashboard Widget', // Title.
        'example_dashboard_widget_function' // Display function.
    );  
}
add_action( 'wp_dashboard_setup', 'example_add_dashboard_widgets' );

function example_dashboard_widget_function() {
    $items_to_show = 10;
    $counter       = 0;
    $recent_posts  = wp_get_recent_posts( [
        'post_status' => 'publish',
        'numberposts' => '50', // More than we want, but tries to ensure we have enough items to display.
        'post_type'   => 'post'
    ] );

    echo '<table>
                    <tr>
                        <th>Post Title</th>
                        <th>Author</th>
                        <th>Moderated by</th>
                    </tr>';

    foreach ( $recent_posts as $recent ) {
        $post_author  = get_user_by( 'id', $recent['post_author'] );
        $last_user_id = get_post_meta( $recent['ID'], '_edit_last', true );
        $last_user    = get_userdata( $last_user_id );

        // Bail out of the loop if we've shown enough items.
        if ( $counter >= $items_to_show ) {
            break;
        }

        // Skip display of items where the author is the same as the moderator:
        if ( $post_author->display_name === $last_user->display_name ) {
            continue;
        }

        echo '<tr><td><a href="' . get_permalink( $recent['ID'] ) . '" title="Read: ' . 
            esc_attr( $recent['post_title'] ).'" >' .   $recent['post_title'].'</a></td>';
        echo '<td>'. esc_html( $post_author->display_name ) .'</td>';

        echo '<td>'. esc_html( $last_user->display_name ) .'</td></tr>';

        $counter++;
    }
    echo '</table>';
}

Edit based on feedback from comment: This code skips the display of items where $post_author->display_name === $last_user->display_name. It's not the most efficient code though, since we're querying more items than needed. This is done because we'll skip some items and we want to (try) to ensure that there are at least 10 items to display ($items_to_show = 10).

3
  • +1 for fixing the OP's code snippet, to display the last moderator, but it looks like OP wants a subset of _edit_last, i.e. only those that last published it, if I understand the question correctly.
    – birgire
    Commented Nov 20, 2016 at 23:39
  • Thanks dave i'll be using this code, it works nicely for me. One last wish if you can guide me: I want to exclude from the for the case when $post_author->display_name == $last_user->display_name. This happens when the person has an author role his post is not moderated he publish it himself, so those shouldn't be considered. I think a simple IF could do it but i'm not smart enough to pull it off. If you can't it's fine too! Thanks for the help. Commented Nov 21, 2016 at 0:46
  • Hi @MichaelRogers, I added the code to handle your request. It's not the prettiest thing ever, since this logic is not accounted for in the query, but it worked for me when I tested it. Commented Nov 21, 2016 at 7:59
3

We could save the id of the user that last published a post, as the value of the _wpse_user_last_published post meta key, with:

add_action( 'transition_post_status', function( $new_status, $old_status, $post )
{
    if( 
           $new_status !== $old_status 
        && 'publish' === $new_status 
        && 'post' === get_post_type( $post ) 
    )
        update_post_meta( $post->ID, '_wpse_user_last_published', get_current_user_id() );

}, 10, 3 );

Then you could fetch this info when you list your posts.

For more detail and faster search, it might be better to create a custom log table.

3
  • I like this answer because it goes above and beyond merely reproducing the functionality of get_the_modified_author(), which only grabs the last user to update the post. Commented Nov 20, 2016 at 23:43
  • Maybe if OP means the latest moderator after all, then you've already solved the problem and this publish-only data storing approach will be unneccessary ;-) @DaveRomsey
    – birgire
    Commented Nov 20, 2016 at 23:49
  • For the record i used the_modified_author because i couldn't think of a better way. This is nice but i'll pick the simpler approach. Because i'd say 99% of the time the person that approved it is the latest person to edit it. And by the time someone else edit the post, it's no longer on the latest 10 so it's irrelevant to me. Commented Nov 21, 2016 at 0:41

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.