wp_post_mime_type_where

Convert MIME types into SQL.

Description

(string) wp_post_mime_type_where( (string|array) $post_mime_types, (string) $table_alias = '' ); 

Returns (string)

The SQL AND clause for mime searching.

Parameters (2)

0. $post_mime_types (string|array)
List of mime types or comma separated string of mime types.
1. $table_alias — Optional. (string) => ''
Specify a table alias, if needed. Default empty.

Usage

  1. if ( !function_exists( 'wp_post_mime_type_where' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // List of mime types or comma separated string 
  5. // of mime types. 
  6. $post_mime_types = null; 
  7.  
  8. // Optional. Specify a table alias, if needed. 
  9. // Default empty. 
  10. $table_alias = ''; 
  11.  
  12. // NOTICE! Understand what this does before running. 
  13. $result = wp_post_mime_type_where($post_mime_types, $table_alias); 
  14.  

Defined (1)

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

/wp-includes/post.php  
  1. function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { 
  2. $where = ''; 
  3. $wildcards = array('', '%', '%/%'); 
  4. if ( is_string($post_mime_types) ) 
  5. $post_mime_types = array_map('trim', explode(', ', $post_mime_types)); 
  6.  
  7. $wheres = array(); 
  8.  
  9. foreach ( (array) $post_mime_types as $mime_type ) { 
  10. $mime_type = preg_replace('/\s/', '', $mime_type); 
  11. $slashpos = strpos($mime_type, '/'); 
  12. if ( false !== $slashpos ) { 
  13. $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos)); 
  14. $mime_subgroup = preg_replace('/[^-*.+a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1)); 
  15. if ( empty($mime_subgroup) ) 
  16. $mime_subgroup = '*'; 
  17. else 
  18. $mime_subgroup = str_replace('/', '', $mime_subgroup); 
  19. $mime_pattern = "$mime_group/$mime_subgroup"; 
  20. } else { 
  21. $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type); 
  22. if ( false === strpos($mime_pattern, '*') ) 
  23. $mime_pattern .= '/*'; 
  24.  
  25. $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern); 
  26.  
  27. if ( in_array( $mime_type, $wildcards ) ) 
  28. return ''; 
  29.  
  30. if ( false !== strpos($mime_pattern, '%') ) 
  31. $wheres[] = empty($table_alias) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; 
  32. else 
  33. $wheres[] = empty($table_alias) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; 
  34. if ( !empty($wheres) ) 
  35. $where = ' AND (' . join(' OR ', $wheres) . ') '; 
  36. return $where;