wp_ajax_find_posts

Ajax handler for querying posts for the Find Posts modal.

Description

wp_ajax_find_posts(); 

Usage

  1. if ( !function_exists( 'wp_ajax_find_posts' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_find_posts(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_find_posts() { 
  2. check_ajax_referer( 'find-posts' ); 
  3.  
  4. $post_types = get_post_types( array( 'public' => true ), 'objects' ); 
  5. unset( $post_types['attachment'] ); 
  6.  
  7. $s = wp_unslash( $_POST['ps'] ); 
  8. $args = array( 
  9. 'post_type' => array_keys( $post_types ),  
  10. 'post_status' => 'any',  
  11. 'posts_per_page' => 50,  
  12. ); 
  13. if ( '' !== $s ) 
  14. $args['s'] = $s; 
  15.  
  16. $posts = get_posts( $args ); 
  17.  
  18. if ( ! $posts ) { 
  19. wp_send_json_error( __( 'No items found.' ) ); 
  20.  
  21. $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>'; 
  22. $alt = ''; 
  23. foreach ( $posts as $post ) { 
  24. $title = trim( $post->post_title ) ? $post->post_title : __( '(no title)' ); 
  25. $alt = ( 'alternate' == $alt ) ? '' : 'alternate'; 
  26.  
  27. switch ( $post->post_status ) { 
  28. case 'publish' : 
  29. case 'private' : 
  30. $stat = __('Published'); 
  31. break; 
  32. case 'future' : 
  33. $stat = __('Scheduled'); 
  34. break; 
  35. case 'pending' : 
  36. $stat = __('Pending Review'); 
  37. break; 
  38. case 'draft' : 
  39. $stat = __('Draft'); 
  40. break; 
  41.  
  42. if ( '0000-00-00 00:00:00' == $post->post_date ) { 
  43. $time = ''; 
  44. } else { 
  45. /** translators: date format in table columns, see https://secure.php.net/date */ 
  46. $time = mysql2date(__('Y/m/d'), $post->post_date); 
  47.  
  48. $html .= '<tr class="' . trim( 'found-posts ' . $alt ) . '"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="' . esc_attr($post->ID) . '"></td>'; 
  49. $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"; 
  50.  
  51. $html .= '</tbody></table>'; 
  52.