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

In the Media Library, when I go in to attach a media item to a post, I know I can use “Attach” to pull up the “Find Posts or Pages” pop up.

This brings the following list of post title, date and status.
find posts or pages results

Read More

I wonder if there is a way to have the results show the post ID as well?

It might have something to do with the find_posts_div function, but I’m not sure how to apply the proper filters.

Related posts

Leave a Reply

1 comment

  1. 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