Create Recent Posts Custom Dashboard Widget
In this article, we will learn how to create a custom dashboard widget to display recent posts without plugin. You can do it simply by just adding some piece of codes in functions.php file.
The snippet I have shared in this article will help you to create a custom dashboard widget to display most recent Posts with Title, Author Name, and Post Date.
The WordPress dashboard is where you go to manage your site and publish new content. However, it doesn’t include a simple way to view recent posts right off the bat. The Recent Posts widget lets you see the list of most recent posts on your site without having to switch tabs.
When you are using WordPress as a CMS then you may have multiple authors writing and publishing content on your website. If you want to filter through all the published content, the easiest and fastest way would be by looking at the most recent posts right into your admin dashboard. My snippet will help you to identify which of your author recently created a post with publish date. This can save you time and help your workflow.
Also check:
Display Post ID column in Post list WordPress
Remove website URL field from WordPress comment form
Change WordPress Admin Dashboard Footer Text
Steps to Create Most Recent Posts Dashboard Widget without Plugin
Before starting, I will recommend you:
- Take a full backup of your website.
- If you are not comfortable editing functions.php file then install this free plugin.
- Use a child theme to make customizations. Use this free plugin to create a child theme (you can delete the plugin after creating child theme, but do not delete the parent theme).
1). Login to your WordPress dashboard. Go to Appearance > Theme File Editor.
2). Access “Theme Functions” or functions.php file.
3). Add below code in functions.php file.
/**
* create wordpress dashboard widget for latest post
*/
function wpamit_recent_posts_dw() {
?>
<ol>
<?php
global $post;
$args = array( 'numberposts' => 5 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><em> by <?php the_author_meta( $field = 'nickname'); ?> on <?php echo get_the_date(); ?></em></li>
<?php endforeach; ?>
</ol>
<?php
}
function add_wpamit_recent_posts_dw() {
wp_add_dashboard_widget( 'wpamit_recent_posts_dw', __( 'Recent Posts' ), 'wpamit_recent_posts_dw' );
}
add_action('wp_dashboard_setup', 'add_wpamit_recent_posts_dw' );
You can define the number of post to display. Here I have set it to 5. You can change the value in line 9 as per your requirements. Line 9 is highlighted.
I hope you learned how to create recent posts dashboard widget without plugin. If you need any help, please comment below.