Using WordPress ‘get_meta_sql’ PHP filter

The get_meta_sql WordPress PHP filter allows you to modify the meta query’s generated SQL.

Usage

add_filter('get_meta_sql', 'your_custom_function', 10, 6);

function your_custom_function($sql, $queries, $type, $primary_table, $primary_id_column, $context) {
    // your custom code here
    return $sql;
}

Parameters

  • $sql (string[]): Array containing the query’s JOIN and WHERE clauses.
  • $queries (array): Array of meta queries.
  • $type (string): Type of meta. Possible values include but are not limited to ‘post’, ‘comment’, ‘blog’, ‘term’, and ‘user’.
  • $primary_table (string): Primary table.
  • $primary_id_column (string): Primary column ID.
  • $context (object): The main query object that corresponds to the type, for example a WP_Query, WP_User_Query, or WP_Site_Query.

More information

See WordPress Developer Resources: get_meta_sql

Examples

Exclude certain post meta from the query

This example excludes posts with a specific post meta key.

add_filter('get_meta_sql', 'exclude_specific_post_meta', 10, 6);

function exclude_specific_post_meta($sql, $queries, $type, $primary_table, $primary_id_column, $context) {
    if ('post' === $type) {
        $sql['where'] .= " AND NOT EXISTS (SELECT * FROM wp_postmeta WHERE wp_postmeta.post_id = {$primary_table}.{$primary_id_column} AND wp_postmeta.meta_key = 'exclude_me')";
    }
    return $sql;
}

Modify user meta query

This example modifies the user meta query to include users with a specific meta key and value.

add_filter('get_meta_sql', 'modify_user_meta_query', 10, 6);

function modify_user_meta_query($sql, $queries, $type, $primary_table, $primary_id_column, $context) {
    if ('user' === $type) {
        $sql['where'] .= " AND EXISTS (SELECT * FROM wp_usermeta WHERE wp_usermeta.user_id = {$primary_table}.{$primary_id_column} AND wp_usermeta.meta_key = 'special_user' AND wp_usermeta.meta_value = 'yes')";
    }
    return $sql;
}

Filter comments by custom meta

This example filters comments to only show those with a specific meta key and value.

add_filter('get_meta_sql', 'filter_comments_by_custom_meta', 10, 6);

function filter_comments_by_custom_meta($sql, $queries, $type, $primary_table, $primary_id_column, $context) {
    if ('comment' === $type) {
        $sql['where'] .= " AND EXISTS (SELECT * FROM wp_commentmeta WHERE wp_commentmeta.comment_id = {$primary_table}.{$primary_id_column} AND wp_commentmeta.meta_key = 'approved_comment' AND wp_commentmeta.meta_value = 'yes')";
    }
    return $sql;
}

Modify term meta query

This example modifies the term meta query to include terms with a specific meta key and value.

add_filter('get_meta_sql', 'modify_term_meta_query', 10, 6);

function modify_term_meta_query($sql, $queries, $type, $primary_table, $primary_id_column, $context) {
    if ('term' === $type) {
$sql['where'] .= " AND EXISTS (SELECT * FROM wp_termmeta WHERE wp_termmeta.term_id = {$primary_table}.{$primary_id_column} AND wp_termmeta.meta_key = 'featured_term' AND wp_termmeta.meta_value = 'yes')";
}
return $sql;
}

Example 5: Modify blog meta query for multisite
This example modifies the blog meta query to include blogs with a specific meta key and value in a multisite setup.

add_filter('get_meta_sql', 'modify_blog_meta_query', 10, 6);

function modify_blog_meta_query($sql, $queries, $type, $primary_table, $primary_id_column, $context) {
    if ('blog' === $type) {
        $sql['where'] .= " AND EXISTS (SELECT * FROM wp_sitemeta WHERE wp_sitemeta.site_id = {$primary_table}.{$primary_id_column} AND wp_sitemeta.meta_key = 'featured_blog' AND wp_sitemeta.meta_value = 'yes')";
    }
    return $sql;
}