Title: wp_ajax_find_posts
Published: April 25, 2014
Last modified: August 20, 2026

---

# wp_ajax_find_posts()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#see-also)
 * [Source](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#wp--skip-link--target)

Handles querying posts for the Find Posts modal via AJAX.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#description)󠁿

### 󠀁[See also](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#see-also)󠁿

 * window.findPosts

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#source)󠁿

    ```php
    function wp_ajax_find_posts() {
    	check_ajax_referer( 'find-posts' );

    	$post_types = get_post_types( array( 'public' => true ), 'objects' );
    	unset( $post_types['attachment'] );

    	$args = array(
    		'post_type'      => array_keys( $post_types ),
    		'post_status'    => 'any',
    		'posts_per_page' => 50,
    	);

    	$search = wp_unslash( $_POST['ps'] );

    	if ( '' !== $search ) {
    		$args['s'] = $search;
    	}

    	$posts = get_posts( $args );

    	if ( ! $posts ) {
    		wp_send_json_error( __( 'No items found.' ) );
    	}

    	$html      = '<table class="widefat"><thead><tr><th class="found-radio"><br /></th><th>' . __( 'Title' ) . '</th><th class="no-break">' . __( 'Type' ) . '</th><th class="no-break">' . __( 'Date' ) . '</th><th class="no-break">' . __( 'Status' ) . '</th></tr></thead><tbody>';
    	$alternate = '';
    	foreach ( $posts as $post ) {
    		$title     = trim( $post->post_title ) ? $post->post_title : __( '(no title)' );
    		$alternate = ( 'alternate' === $alternate ) ? '' : 'alternate';

    		switch ( $post->post_status ) {
    			case 'publish':
    			case 'private':
    				$stat = __( 'Published' );
    				break;
    			case 'future':
    				$stat = __( 'Scheduled' );
    				break;
    			case 'pending':
    				$stat = __( 'Pending Review' );
    				break;
    			case 'draft':
    				$stat = __( 'Draft' );
    				break;
    		}

    		if ( '0000-00-00 00:00:00' === $post->post_date ) {
    			$time = '';
    		} else {
    			/* translators: Date format in table columns, see https://www.php.net/manual/datetime.format.php */
    			$time = mysql2date( __( 'Y/m/d' ), $post->post_date );
    		}

    		$html .= '<tr class="' . trim( 'found-posts ' . $alternate ) . '"><td class="found-radio"><input type="radio" id="found-' . $post->ID . '" name="found_post_id" value="' . esc_attr( $post->ID ) . '"></td>';
    		$html .= '<td><label for="found-' . $post->ID . '">' . esc_html( $title ) . '</label></td><td class="no-break">' . esc_html( $post_types[ $post->post_type ]->labels->singular_name ) . '</td><td class="no-break">' . esc_html( $time ) . '</td><td class="no-break">' . esc_html( $stat ) . ' </td></tr>' . "\n\n";
    	}

    	$html .= '</tbody></table>';

    	wp_send_json_success( $html );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/ajax-actions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-admin/includes/ajax-actions.php#L2268)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-admin/includes/ajax-actions.php#L2268-L2328)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#related)󠁿

| Uses | Description | 
| [mysql2date()](https://developer.wordpress.org/reference/functions/mysql2date/)`wp-includes/functions.php` |

Converts given MySQL date string into a different format.

  | 
| [get_posts()](https://developer.wordpress.org/reference/functions/get_posts/)`wp-includes/post.php` |

Retrieves an array of the latest posts, or posts matching the given criteria.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [wp_unslash()](https://developer.wordpress.org/reference/functions/wp_unslash/)`wp-includes/formatting.php` |

Removes slashes from a string or recursively removes slashes from strings within an array.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [esc_html()](https://developer.wordpress.org/reference/functions/esc_html/)`wp-includes/formatting.php` |

Escaping for HTML blocks.

  | 
| [check_ajax_referer()](https://developer.wordpress.org/reference/functions/check_ajax_referer/)`wp-includes/pluggable.php` |

Verifies the Ajax request to prevent processing requests external of the blog.

  | 
| [wp_send_json_error()](https://developer.wordpress.org/reference/functions/wp_send_json_error/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating failure.

  | 
| [wp_send_json_success()](https://developer.wordpress.org/reference/functions/wp_send_json_success/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating success.

  | 
| [get_post_types()](https://developer.wordpress.org/reference/functions/get_post_types/)`wp-includes/post.php` |

Gets a list of all registered post type objects.

  |

[Show 8 more](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_find_posts/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.1.0](https://developer.wordpress.org/reference/since/3.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_ajax_find_posts%2F)
before being able to contribute a note or feedback.