get_meta_sql()获取meta的添加筛选条件的SQL查询语句

目录

描述

Given a meta query, generates SQL clauses to be appended to a main query.

get_meta_sql() 描述

用法

<?php get_meta_sql$meta_query$type$primary_table$primary_id_column$context ); ?>

get_meta_sql() 用法

参数

$meta_query

(array) (必填) A meta query.

默认值: None

$type

(string) (必填) Type of meta.

默认值: None

$primary_table

(string) (必填) Database table you want to append the query to.

默认值: None

$primary_id_column

(string) (必填) Database table primary id column.

默认值: None

$context

(object) (可选) The main query object.

默认值:

get_meta_sql() 参数

返回值

(array) array( 'join' => $join_sql, 'where' => $where_sql ).

get_meta_sql() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php  
$meta_query = array(
	array(
		'key' => 'color',
		'value' => 'blue',
		'compare' => 'NOT LIKE'
	)
);
global $wpdb;
$meta_sql = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' );

Output depending on the meta query:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

Array
(
    [join] =>  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    [where] =>  AND ( (wp_postmeta.meta_key = 'color' AND CAST(wp_postmeta.meta_value AS CHAR) NOT LIKE '%blue%') )
)

get_meta_sql() 示例

历史

添加于 版本: 3.2.0

get_meta_sql() 历史

源文件

get_meta_sql() 函数的代码位于 wp-includes/meta.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Given a meta query, generates SQL clauses to be appended to a main query.
 *
 * @since 3.2.0
 *
 * @see WP_Meta_Query
 *
 * @param array $meta_query         A meta query.
 * @param string $type              Type of meta.
 * @param string $primary_table     Primary database table name.
 * @param string $primary_id_column Primary ID column name.
 * @param object $context           Optional. The main query object
 * @return array Associative array of `JOIN` and `WHERE` SQL.
 */
function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
	$meta_query_obj = new WP_Meta_Query( $meta_query );
	return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
}

get_meta_sql() 源文件