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.

13 comments

    • Peter Knight says:

      Sorry Doug, I pasted an incomplete line, check that your action hook has extra parameters like so
      add_action( "manage_posts_custom_column" , "last_modified_column", 10, 2 );
      the 10 stands for priority, the 2 stands for the amount of arguments it takes. Let me know if you’ve got it to work

  1. Olorin says:

    Hello Peter,

    No data is displayed in the column, do you know why ? I can sort the column, it’s working as if there are values in the column, but nothing is displayed in each row. Is there a part missing in the code ? Like echo ‘the_modified_time( “F j, Y” ); ?

  2. Dadit says:

    Hi …

    I implemented your code to my carousel/banner images where each image has a start date andan end date of showing.  That is, I am able to sort them by a start date and an end data, in both cases are ascending and descending.  Works well!

    Of a total of 25 images I have, however, there are five images that do not have both a start date and an end date.  What happens when I sort them by either a start or an end date, the five images that do not have these data are not sorted and disappeared; only 18 images being sorted.

    This does not follow the spreadsheet logic.  For example, in microsoft excel, those empty cells still get sorted but will be placed at the end or at the beginning of the order.  Why do yo think these 5 images without data that being sorted disappeared?

    Hope this makes sense and you can help?

    Thanks!

  3. Ralf says:

    Hi Peter,

    Thanks a lot for that post, works great !

    I am wondering do you know a way how I could sort currency values like “$ 20,00” and so forth correctly?

    It seems by default the search is only alphabetical, right?

    Thanks and best regards,

    Ralf

    • Peter Knight says:

      Hi Ralf, depends on how the values are stored in the database. Sorting can be done numerically but if the values are saved as strings with the $ and mixed ./, notation it will be harder to do a sort. Having said that, I know Woocommerce has this functionality in their orders screen, that’s probably where I’d look to figure out how its done there.

Leave a Reply

Your email address will not be published. Required fields are marked *