Quantcast
Viewing all articles
Browse latest Browse all 30

How To Show A Sortable Last Modified Column In The Manage Posts Screen [WP]

So you want to quickly find posts that were recently edited? Here’s a quick WordPress guide to adding a sortable column to the post managing screen that displays the last modified date. This example can be easily modified to work with another type of field or a specific post type.

Step 1

So to start, let’s register the column we want to have appear. We’ll have a function to add our column name to the columns array. A filter hook will call this function.

function add_last_modified_column($columns) {
    return array_merge( $columns,
              array("last_modified" => __("Last Modified")) );
}
add_filter("manage_posts_columns" , "add_last_modified_column");

Step 2

Let’s create a function that will output our column’s data and hook it into the posts column. Depending on what kind of content screen (pages/posts/custom posts) you want to show the column, there are different action hooks that you can apply. To target a specific custom post type you use the following naming convention:

manage_${post_type}_posts_custom_column() // for posts (non-hierarchical)
manage_pages_custom_column() // for pages (hierarchical)

The basic one is the general manage_posts_columns action hook, which we’ll be using here.

function last_modified_column( $column, $post_id ){
  if( "last_modified" == $column ) {
    the_modified_time( "F j, Y" );
  }
}
add_action( "manage_posts_custom_column" , "last_modified_column", 10, 2 );

What’s happening here is that when the columns are being processed one by one, when it matches the column called ‘last_modified’  it will output the last modified time of the post. We’re using the_modified_time() function to render the time the post was last updated. We can control the formatting date and time by passing parameters you might already be familiar with. The action hook registers our function so that it’s run when it processes the columns.

Step 3

Next we want to tell WordPress that this column should be sortable by registering our interest. We’ll be using an action hook that follows a familiar naming convention.

function last_modified_column_register_sortable( $columns ) {
	$columns["last_modified"] = "last_modified";

	return $columns;
}
add_filter( "manage_edit-post_sortable_columns", "last_modified_column_register_sortable" ); // replace "post" with the slug of your custom post type

Step 4

We need one extra step to make this column sortable. To do this will create a function that modifies the request with an extra query parameter that enables the sorting.

function sort_column_by_modified( $vars ){
	if ( isset( $vars["orderby"] ) && "last_modified" == $vars["orderby"] ) {
		$vars = array_merge( $vars, array(
			"orderby" => "modified"
		) );
	}
	return $vars;
}
add_filter( "request", "sort_column_by_modified" );

To get it all working, we now only have to make sure this code is loaded by adding it to a custom plugin, or the theme’s functions.php file. Personally I would recommend creating a small custom plugin for this. The great thing about having the last modified data at a glance is that you can see what posts were recently edited, and which haven’t been touched in a long time.

Image may be NSFW.
Clik here to view.


Viewing all articles
Browse latest Browse all 30

Trending Articles