4

I run a multiple author site and I have created a plugin in wp-admin where authors can connect and collaborate with each other. To enhance it, I want to display a list of latest posts, this list should look like this:

Latest posts

As the picture shows, the list should contain the latest 10 posts with the Username, the post title linked and when it was posted (either how long ago or the exact time of post).

The HTML for this should be:

<table class="widefat">

            <thead><tr><th scope="col">User</th><th scope="col">Post</th><th scope="col">Time</th></tr></thead>
            <tfoot><tr><th scope="col">User</th><th scope="col">Post</th><th scope="col">Time</th></tr></tfoot>
            <tbody>
                <tr>
                    <td title="Username">Username</td>
                    <td><a href="URL">Post title</a></td>
                    <td title="2012-11-07 16:16:37">8 hours ago</td>
                </tr>       
            </tbody>
    </table>

How can I create a list like this? Note that this list will be used in wp-admin only.

1 Answer 1

4

All you need is a table with class='widefat' and get_posts(). Then you run through the results (if there are any) and print the table rows.

Here is a very simple example as a dashboard widget. Note the lack of I18n – it is not ready to use!

<?php
/**
 * Plugin Name: Last posts table
 * Plugin URI:  http://wordpress.stackexchange.com/q/71887/73
 */

add_action( 'wp_dashboard_setup', 'wpse_71887_register_dashboard_widget' );

function wpse_71887_register_dashboard_widget()
{
    wp_add_dashboard_widget(
        __FUNCTION__,
        'Last Posts',
        'wpse_71887_render_dashboard_widget'
    );
}

function wpse_71887_render_dashboard_widget()
{
    $header = '
        <tr>
            <th>User</th>
            <th>Post</th>
            <th>Time</th>
        </tr>';

    print "<table class='widefat'>
    <thead>$header</thead>
    <tfoot>$header</tfoot>
    <tbody>";

    // get 10 last private and published posts
    $posts = get_posts(
        array (
            'numberposts' => 10,
            'post_type'   => array ( 'post', 'page' )
        )
    );

    if ( ! $posts )
    {
        print '<tr><td cols="3">No posts found. <a href="'
            . admin_url( 'post-new.php' ) . '">Write one!</a>';
    }
    else
    {
        foreach ( $posts as $post )
        {
            printf(
                '<tr>
                    <td>%1$s</td>
                    <td><a href="%2$s">%3$s</a></td>
                    <td>%4$s ago</td>
                </tr>',
                esc_html( get_user_by( 'id', $post->post_author )->display_name ),
                get_permalink( $post->ID ),
                esc_html( $post->post_title ),
                human_time_diff( mysql2date( 'U', $post->post_date ) )
            );
        }
    }

    print '</table>';
}

Result

enter image description here

5
  • @torcho Can you please elaborate on I18n? I am not familiar with that term. Nov 8, 2012 at 14:04
  • I18n is a short term for Internationalization. We have a tag synonym for that: localization. It means the strings are translatable. I did not implement that in this sample code, so it will work in English only.
    – fuxia
    Nov 8, 2012 at 14:06
  • Please check my updated answer. I want to insert this into my own plugin (not dashboard), so I added the function wpse_71887_render_dashboard_widget() but it did not display anything. I am guessing that I did not execute the function? What am I missing here? Nov 8, 2012 at 16:23
  • Ignore my stupidity, it works now. However, there remains one issue, why is pages added to the list? Is there any way to exclude them? Nov 8, 2012 at 16:27
  • 2
    See the line 'post_type' => array ( 'post', 'page' ). You can remove page from that array.
    – fuxia
    Nov 8, 2012 at 16:36

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.