函数文档

wp_post_mime_type_where()

💡 云策文档标注

概述

wp_post_mime_type_where() 函数用于将 MIME 类型转换为 SQL WHERE 子句,主要用于在 WordPress 中构建附件查询条件。它处理 MIME 类型字符串或数组,并生成相应的 SQL 语句以支持精确匹配或通配符搜索。

关键要点

  • 函数接受 MIME 类型参数,可以是字符串(逗号分隔)或数组,并可选指定表别名。
  • 内部处理包括清理 MIME 类型、解析主类型和子类型、支持通配符(如 * 转换为 %)以生成 LIKE 或 = 条件。
  • 返回一个 SQL AND 子句字符串,可直接用于查询构建,例如在 WP_Query 或 wp_count_attachments() 中。

代码示例

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;
}

注意事项

  • 函数自 WordPress 2.5.0 版本引入,主要用于内部查询,如 WP_Query 和 wp_count_attachments()。
  • 处理 MIME 类型时,会自动清理无效字符并支持通配符转换,确保 SQL 语句的安全性。
  • 如果传入的 MIME 类型匹配特定通配符(如空字符串或 %),函数可能返回空字符串,表示无限制条件。

📄 原文内容

Converts MIME types into SQL.

Parameters

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

Return

string The SQL AND clause for mime searching.

Source

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

Version Description
2.5.0 Introduced.