Show Post ID in “Find Posts or Pages” box in Media Library?

You cannot do that with pure PHP. The table is created in wp-admin/includes/ajax-actions.php::wp_ajax_find_posts(), and there is no filter.

But look at the radio buttons:

name="found_post_id" value="' . esc_attr($post->ID)

You can print a script on the action admin_footer-upload.php that extracts the post ID from the values and adds a new column to the table.

Here is a working example as a plugin. The code is not exactly beautiful, but it works and it should speak for itself:

<?php
/**
 * Plugin Name: Extend Media Find Posts Table
 */

add_action( 'admin_footer-upload.php', 't5_add_id_to_post_search' );

function t5_add_id_to_post_search()
{
?>
<script>
jQuery(function($) {
    // bind ajaxStop() to any unique element.
    $('#find-posts-submit').ajaxStop(function() {
        var table = $('#find-posts-response table');
        // add the heading
        $(table).find('thead tr').append($('<th>ID</th>').css('width', '2em'));
        // take ID from input and append as 'td'
        $(table).find('tbody tr').each(function(){
            $(this).append('<td>' + $(this).find('input').val() + '</td>');
        });
    });
});
</script>
<?php
}

Result:

enter image description here

Leave a Comment