0

I found the following code at bytefreaks.net. It adds a sort-able column to edit.php, but the column appears only on Posts and not on Pages (...?post_type=page)

bytefreaks suggested putting it in functions.php but I wanted to avoid update problems so I installed the "My Custom Functions" plugin and put the code there.

I don't know WordPress well enough to fully understand each function. Can anyone tell me what I need to do to make this code work for Pages as well as Posts?

// Register the column
function post_modified_column_register( $columns ) {
    $columns['post_modified'] = __( 'Modified', 'mytextdomain' );
    return $columns;
}
add_filter( 'manage_edit-post_columns', 'post_modified_column_register' );

// Display the column content
function post_modified_column_display( $column_name, $post_id ) {
    if ( 'post_modified' != $column_name ){
        return;
    }
    $post_modified = get_post_field('post_modified', $post_id);
    if ( !$post_modified ){
        $post_modified = '' . __( 'undefined', 'mytextdomain' ) . '';
    }
    echo $post_modified;
}
add_action( 'manage_posts_custom_column', 'post_modified_column_display', 10, 2 );

// Register the column as sortable
function post_modified_column_register_sortable( $columns ) {
    $columns['post_modified'] = 'post_modified';
    return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'post_modified_column_register_sortable' );
1

Check the documentation for the Plugin API/Filter Reference

Looks like you need to duplicate your actions and filters for manage_pages_custom_columns/manage_edit-page_columns etc etc. Fair warning: I've not vetted this.

See below

https://codex.wordpress.org/Plugin_API/Filter_Reference/manage_edit-post_type_columns https://codex.wordpress.org/Plugin_API/Action_Reference/manage_pages_custom_column

Here's the resulting code that works:

// Custom Functions to add SORT BY MODIFIED to post and page editor.
// POSTS
// Register the column
function post_modified_column_register( $columns ) {
    $columns['post_modified'] = __( 'Modified', 'mytextdomain' );
    return $columns;
}
add_filter( 'manage_edit-post_columns', 'post_modified_column_register' );

// Display the column content
function post_modified_column_display( $column_name, $post_id ) {
    if ( 'post_modified' != $column_name ){
        return;
    }
    $post_modified = get_post_field('post_modified', $post_id);
    if ( !$post_modified ){
        $post_modified = '' . __( 'undefined', 'mytextdomain' ) . '';
    }
    echo $post_modified;
}
add_action( 'manage_posts_custom_column', 'post_modified_column_display', 10, 2 );

// Register the column as sortable
function post_modified_column_register_sortable( $columns ) {
    $columns['post_modified'] = 'post_modified';
    return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'post_modified_column_register_sortable' );
//PAGES
// Register the column
function page_modified_column_register( $columns ) {
    $columns['page_modified'] = __( 'Modified', 'mytextdomain' );
    return $columns;
}
add_filter( 'manage_edit-page_columns', 'page_modified_column_register' );

// Display the column content
function page_modified_column_display( $column_name, $page_id ) {
    if ( 'page_modified' != $column_name ){
        return;
    }
    $page_modified = get_post_field('post_modified', $page_id);
    if ( !$page_modified ){
        $page_modified = '' . __( 'undefined', 'mytextdomain' ) . '';
    }
    echo $page_modified;
}
add_action( 'manage_pages_custom_column', 'page_modified_column_display', 10, 2 );

// Register the column as sortable
function page_modified_column_register_sortable( $columns ) {
    $columns['page_modified'] = 'page_modified';
    return $columns;
}
add_filter( 'manage_edit-page_sortable_columns', 'page_modified_column_register_sortable' );

// end SORT BY MODIFIED
  • I'll give that a shot. I wondered if that might be it, but didn't want to charge blindly into it. – TecBrat Oct 14 '15 at 16:01
  • Close already! Just doing it the lazy way, blindly replacing "post" with "page" in the code and adding it to the end got me the column, but with an error. Errors are generally helpful, so off I go... – TecBrat Oct 14 '15 at 16:05
  • They sure are. FYI, you might also need not duplicate your functions. You could just try duplicating the add_action and add_filter using the page related filters/actions and the same function as your post filters/actions second parameter. Depends if there are any variances between the two hooks for pages/posts. Good luck! – Spongsta Oct 14 '15 at 16:09
  • I'm going to edit your answer, but if you find the edit too major, I will accept a rollback: – TecBrat Oct 14 '15 at 16:17
  • Looks good to me. NB: Answer code provided by @TecBrat – Spongsta Oct 14 '15 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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