Using WordPress ‘find_posts_div()’ PHP function

The find_posts_div() WordPress PHP function is used to output the modal window that attaches media to posts or pages in the media-listing screen.


Below is a simple example of how the function can be used:


In this example, 'my_action' is the custom action that will be returned when the media is found and attached to a post or page.


  • $found_action (string) (Optional): This parameter represents the value of the ‘found_action’ input field. Default is an empty string (”).

More information

See WordPress Developer Resources: find_posts_div()


Basic Use

The function can be used directly in your theme or plugin to display the media attachment modal.

// Display the media attachment modal

Custom Action

You can provide a custom action that will be returned when the media is found.

// Display the media attachment modal with a custom action

Use in a Function

The function can be used within another function, for instance, when building a custom media library for your theme.

function my_media_library() {
  // Use find_posts_div() function to display media attachment modal

Conditional Use

You can use the function conditionally, for example, only if a certain user role is viewing the page.

if ( current_user_can('editor') ) {
  // Display the media attachment modal for 'editor' role

Use with AJAX

This function can be used in combination with AJAX to load the media modal without refreshing the page.

// Enqueue script
wp_enqueue_script('my_script', get_template_directory_uri() . '/js/my_script.js', array('jquery'), '', true);

// Localize script
wp_localize_script('my_script', 'my_ajax', array(
  'ajax_url' => admin_url('admin-ajax.php')

// Add AJAX action
add_action('wp_ajax_my_action', 'my_ajax_function');
function my_ajax_function() {

In your JavaScript file:

jQuery(document).ready(function($) {
  $.post(my_ajax.ajax_url, {action: 'my_action'}, function(response) {
    // Handle response

In this example, the media modal is loaded via AJAX when the page is ready.