wp_ajax_query_attachments

Ajax handler for querying attachments.

Description

wp_ajax_query_attachments(); 

Usage

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

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_query_attachments() { 
  2. if ( ! current_user_can( 'upload_files' ) ) 
  3.  
  4. $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array(); 
  5. $keys = array( 
  6. 's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',  
  7. 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum' 
  8. ); 
  9. foreach ( get_taxonomies_for_attachments( 'objects' ) as $t ) { 
  10. if ( $t->query_var && isset( $query[ $t->query_var ] ) ) { 
  11. $keys[] = $t->query_var; 
  12.  
  13. $query = array_intersect_key( $query, array_flip( $keys ) ); 
  14. $query['post_type'] = 'attachment'; 
  15. if ( MEDIA_TRASH 
  16. && ! empty( $_REQUEST['query']['post_status'] ) 
  17. && 'trash' === $_REQUEST['query']['post_status'] ) { 
  18. $query['post_status'] = 'trash'; 
  19. } else { 
  20. $query['post_status'] = 'inherit'; 
  21.  
  22. if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) ) 
  23. $query['post_status'] .= ', private'; 
  24.  
  25. // Filter query clauses to include filenames. 
  26. if ( isset( $query['s'] ) ) { 
  27.  
  28. /** 
  29. * Filters the arguments passed to WP_Query during an Ajax 
  30. * call for querying attachments. 
  31. * @since 3.7.0 
  32. * @see WP_Query::parse_query() 
  33. * @param array $query An array of query variables. 
  34. */ 
  35. $query = new WP_Query( $query ); 
  36.  
  37. $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts ); 
  38. $posts = array_filter( $posts ); 
  39.  
  40. wp_send_json_success( $posts );