返回介绍

get_meta_sql()

发布于 2017-09-10 23:34:20 字数 2597 浏览 939 评论 0 收藏 0

get_meta_sql( array $meta_query,  string $type,  string $primary_table,  string $primary_id_column,  object $context = null )

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


description


参数

$meta_query

(array) (Required) A meta query.

$type

(string) (Required) Type of meta.

$primary_table

(string) (Required) Primary database table name.

$primary_id_column

(string) (Required) Primary ID column name.

$context

(object) (Optional) The main query object

Default value: null


返回值

(array) Associative array of JOIN and WHERE SQL.


源代码

File: wp-includes/meta.php

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

更新日志

Versiondescription
3.2.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-meta-query.php: WP_Meta_Query::__construct()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Example

    
    <?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:

    
    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%') )
    )
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文