Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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

share|improve this answer
    
@torcho Can you please elaborate on I18n? I am not familiar with that term. –  Amanda Duke Nov 8 '12 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. –  toscho Nov 8 '12 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? –  Amanda Duke Nov 8 '12 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? –  Amanda Duke Nov 8 '12 at 16:27
2  
See the line 'post_type' => array ( 'post', 'page' ). You can remove page from that array. –  toscho Nov 8 '12 at 16:36
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.