wppaste
WordPress

wp_post_mime_type_where( string|string[] $post_mime_types, string $table_alias = '' ): string

Since
2.5.0
Source
wp-includes/post.php:3765
Converts MIME types into SQL.

Compatibility

WordPress
since 2.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_mime_typesstring|string[]
List of mime types or comma separated string of mime types.
$table_aliasstringoptional
Specify a table alias, if needed.
Default empty.Default: ''

Return value

string
The SQL AND clause for mime searching.

Performance profile

How much work a call to wp_post_mime_type_where() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
12–51

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 95.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_post_mime_type_where() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_string($post_mime_types)12–42none
is_string($post_mime_types)21–51explode(), array_map()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9512–5111
8.59512–5111
8.49512–511136 fewer instructions than PHP 8.3
8.313112–7511
8.213112–7511
8.113112–75114 fewer instructions than PHP 7.4
7.413512–7511

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

  • str_contains()Polyfill for `str_contains()` function added in PHP 8.0.

Used by · 2

Source code

function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {	$where     = '';	$wildcards = array( '', '%', '%/%' );	if ( is_string( $post_mime_types ) ) {		$post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) );	} 	$where_clauses = array(); 	foreach ( (array) $post_mime_types as $mime_type ) {		$mime_type = preg_replace( '/\s/', '', $mime_type );		$slashpos  = strpos( $mime_type, '/' );		if ( false !== $slashpos ) {			$mime_group    = preg_replace( '/[^-*.a-zA-Z0-9]/', '', substr( $mime_type, 0, $slashpos ) );			$mime_subgroup = preg_replace( '/[^-*.+a-zA-Z0-9]/', '', substr( $mime_type, $slashpos + 1 ) );			if ( empty( $mime_subgroup ) ) {				$mime_subgroup = '*';			} else {				$mime_subgroup = str_replace( '/', '', $mime_subgroup );			}			$mime_pattern = "$mime_group/$mime_subgroup";		} else {			$mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type );			if ( ! str_contains( $mime_pattern, '*' ) ) {				$mime_pattern .= '/*';			}		} 		$mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern ); 		if ( in_array( $mime_type, $wildcards, true ) ) {			return '';		} 		if ( str_contains( $mime_pattern, '%' ) ) {			$where_clauses[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'";		} else {			$where_clauses[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'";		}	} 	if ( ! empty( $where_clauses ) ) {		$where = ' AND (' . implode( ' OR ', $where_clauses ) . ') ';	} 	return $where;}

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.