本文整理汇总了PHP中WP_Meta_Query::get_sql方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_Meta_Query::get_sql方法的具体用法?PHP WP_Meta_Query::get_sql怎么用?PHP WP_Meta_Query::get_sql使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_Meta_Query
的用法示例。
在下文中一共展示了WP_Meta_Query::get_sql方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: terms_clauses
/**
* terms_clauses
* Clause updates for term queries.
*
* @param array $pieces The pieces of the sql query
* @param array $taxonomies The taxonomies for the query
* @param array $arguments The arguments for the query
*
* @return array $pieces
*
* @access public
* @static
* @since 1.0
*/
public static function terms_clauses($pieces = array(), $taxonomies = array(), $arguments = array())
{
if (!empty($arguments['meta_query'])) {
$query = new WP_Meta_Query($arguments['meta_query']);
$query->parse_query_vars($arguments);
if (!empty($query->queries)) {
$clauses = $query->get_sql('term', 'tt', 'term_id', $taxonomies);
$pieces['join'] .= $clauses['join'];
$pieces['where'] .= $clauses['where'];
}
}
return $pieces;
}
示例2: array
/**
* @ticket 22096
*/
function test_empty_value_sql() {
global $wpdb;
$query = new WP_Meta_Query();
$the_complex_query['meta_query'] = array(
array( 'key' => 'my_first_key', 'value' => 'my_amazing_value' ),
array( 'key' => 'my_second_key', 'compare' => 'NOT EXISTS' ),
array( 'key' => 'my_third_key', 'value' => array( ), 'compare' => 'IN' ),
);
$query->parse_query_vars( $the_complex_query );
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
// We should have 2 joins - one for my_first_key and one for my_second_key
$this->assertEquals( 2, substr_count( $sql['join'], 'INNER JOIN' ) );
// The WHERE should check my_third_key against an unaliased table
$this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'my_third_key'" ) );
}
示例3: relevanssi_search
//.........这里部分代码省略.........
if (is_array($parent_query)) {
if (!empty($parent_query['parent in'])) {
$valid_values = array();
foreach ($parent_query['parent in'] as $post_in_id) {
if (is_numeric($post_in_id)) {
$valid_values[] = $post_in_id;
}
}
$posts = implode(',', $valid_values);
if (!empty($posts)) {
$query_restrictions .= " AND relevanssi.doc IN (SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ({$posts}))";
}
// Clean: $posts is checked to be integers
}
if (!empty($parent_query['parent not in'])) {
$valid_values = array();
foreach ($parent_query['parent not in'] as $post_not_in_id) {
if (is_numeric($post_not_in_id)) {
$valid_values[] = $post_not_in_id;
}
}
$posts = implode(',', $valid_values);
if (!empty($posts)) {
$query_restrictions .= " AND relevanssi.doc NOT IN (SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ({$posts}))";
}
// Clean: $posts is checked to be integers
}
}
if (is_array($meta_query)) {
$meta_query_restrictions = "";
$mq_vars = array('meta_query' => $meta_query);
$mq = new WP_Meta_Query();
$mq->parse_query_vars($mq_vars);
$meta_sql = $mq->get_sql('post', 'relevanssi', 'doc');
$meta_join = "";
$meta_where = "";
if ($meta_sql) {
$meta_join = $meta_sql['join'];
$meta_where = $meta_sql['where'];
}
$query_restrictions .= $meta_where;
}
if (!empty($date_query)) {
if (is_object($date_query) && method_exists($date_query, 'get_sql')) {
$sql = $date_query->get_sql();
// AND ( the query itself )
$query_restrictions .= " AND relevanssi.doc IN ( SELECT DISTINCT(ID) FROM {$wpdb->posts} WHERE 1 {$sql} )";
// Clean: $sql generated by $date_query->get_sql() query
}
}
if (!$post_type && get_option('relevanssi_respect_exclude') == 'on') {
if (function_exists('get_post_types')) {
$pt_1 = get_post_types(array('exclude_from_search' => '0'));
$pt_2 = get_post_types(array('exclude_from_search' => false));
$post_type = implode(',', array_merge($pt_1, $pt_2));
}
}
if ($post_type) {
if ($post_type == -1) {
$post_type = null;
}
// Facetious sets post_type to -1 if not selected
if (!is_array($post_type)) {
$post_types = esc_sql(explode(',', $post_type));
} else {
$post_types = esc_sql($post_type);
示例4: prepare_query
//.........这里部分代码省略.........
$roles_clauses[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'value' => '"' . $role . '"', 'compare' => 'LIKE');
}
$role_queries[] = $roles_clauses;
}
$role__in_clauses = array('relation' => 'OR');
if (!empty($role__in)) {
foreach ($role__in as $role) {
$role__in_clauses[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'value' => '"' . $role . '"', 'compare' => 'LIKE');
}
$role_queries[] = $role__in_clauses;
}
$role__not_in_clauses = array('relation' => 'AND');
if (!empty($role__not_in)) {
foreach ($role__not_in as $role) {
$role__not_in_clauses[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'value' => '"' . $role . '"', 'compare' => 'NOT LIKE');
}
$role_queries[] = $role__not_in_clauses;
}
// If there are no specific roles named, make sure the user is a member of the site.
if (empty($role_queries)) {
$role_queries[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'compare' => 'EXISTS');
}
// Specify that role queries should be joined with AND.
$role_queries['relation'] = 'AND';
if (empty($this->meta_query->queries)) {
$this->meta_query->queries = $role_queries;
} else {
// Append the cap query to the original queries and reparse the query.
$this->meta_query->queries = array('relation' => 'AND', array($this->meta_query->queries, $role_queries));
}
$this->meta_query->parse_query_vars($this->meta_query->queries);
}
if (!empty($this->meta_query->queries)) {
$clauses = $this->meta_query->get_sql('user', $wpdb->users, 'ID', $this);
$this->query_from .= $clauses['join'];
$this->query_where .= $clauses['where'];
if ($this->meta_query->has_or_relation()) {
$this->query_fields = 'DISTINCT ' . $this->query_fields;
}
}
// sorting
$qv['order'] = isset($qv['order']) ? strtoupper($qv['order']) : '';
$order = $this->parse_order($qv['order']);
if (empty($qv['orderby'])) {
// Default order is by 'user_login'.
$ordersby = array('user_login' => $order);
} elseif (is_array($qv['orderby'])) {
$ordersby = $qv['orderby'];
} else {
// 'orderby' values may be a comma- or space-separated list.
$ordersby = preg_split('/[,\\s]+/', $qv['orderby']);
}
$orderby_array = array();
foreach ($ordersby as $_key => $_value) {
if (!$_value) {
continue;
}
if (is_int($_key)) {
// Integer key means this is a flat array of 'orderby' fields.
$_orderby = $_value;
$_order = $order;
} else {
// Non-integer key means this the key is the field and the value is ASC/DESC.
$_orderby = $_key;
$_order = $_value;
}
示例5: array
//.........这里部分代码省略.........
$this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
}
$this->query_from = "FROM {$wpdb->users}";
$this->query_where = "WHERE 1=1";
// sorting
if (in_array($qv['orderby'], array('nicename', 'email', 'url', 'registered'))) {
$orderby = 'user_' . $qv['orderby'];
} elseif (in_array($qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered'))) {
$orderby = $qv['orderby'];
} elseif ('name' == $qv['orderby'] || 'display_name' == $qv['orderby']) {
$orderby = 'display_name';
} elseif ('post_count' == $qv['orderby']) {
// todo: avoid the JOIN
$where = get_posts_by_author_sql('post');
$this->query_from .= " LEFT OUTER JOIN (\n\t\t\t\tSELECT post_author, COUNT(*) as post_count\n\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t{$where}\n\t\t\t\tGROUP BY post_author\n\t\t\t) p ON ({$wpdb->users}.ID = p.post_author)\n\t\t\t";
$orderby = 'post_count';
} elseif ('ID' == $qv['orderby'] || 'id' == $qv['orderby']) {
$orderby = 'ID';
} else {
$orderby = 'user_login';
}
$qv['order'] = strtoupper($qv['order']);
if ('ASC' == $qv['order']) {
$order = 'ASC';
} else {
$order = 'DESC';
}
$this->query_orderby = "ORDER BY {$orderby} {$order}";
// limit
if ($qv['number']) {
if ($qv['offset']) {
$this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
} else {
$this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']);
}
}
$search = trim($qv['search']);
if ($search) {
$leading_wild = ltrim($search, '*') != $search;
$trailing_wild = rtrim($search, '*') != $search;
if ($leading_wild && $trailing_wild) {
$wild = 'both';
} elseif ($leading_wild) {
$wild = 'leading';
} elseif ($trailing_wild) {
$wild = 'trailing';
} else {
$wild = false;
}
if ($wild) {
$search = trim($search, '*');
}
if (false !== strpos($search, '@')) {
$search_columns = array('user_email');
} elseif (is_numeric($search)) {
$search_columns = array('user_login', 'ID');
} elseif (preg_match('|^https?://|', $search)) {
$search_columns = array('user_url');
} else {
$search_columns = array('user_login', 'user_nicename');
}
$this->query_where .= $this->get_search_sql($search, $search_columns, $wild);
}
$blog_id = absint($qv['blog_id']);
if ('authors' == $qv['who'] && $blog_id) {
$qv['meta_key'] = $wpdb->get_blog_prefix($blog_id) . 'user_level';
$qv['meta_value'] = 0;
$qv['meta_compare'] = '!=';
$qv['blog_id'] = $blog_id = 0;
// Prevent extra meta query
}
$role = trim($qv['role']);
if ($blog_id && ($role || is_multisite())) {
$cap_meta_query = array();
$cap_meta_query['key'] = $wpdb->get_blog_prefix($blog_id) . 'capabilities';
if ($role) {
$cap_meta_query['value'] = '"' . $role . '"';
$cap_meta_query['compare'] = 'like';
}
$qv['meta_query'][] = $cap_meta_query;
}
$meta_query = new WP_Meta_Query();
$meta_query->parse_query_vars($qv);
if (!empty($meta_query->queries)) {
$clauses = $meta_query->get_sql('user', $wpdb->users, 'ID', $this);
$this->query_from .= $clauses['join'];
$this->query_where .= $clauses['where'];
if ('OR' == $meta_query->relation) {
$this->query_fields = 'DISTINCT ' . $this->query_fields;
}
}
if (!empty($qv['include'])) {
$ids = implode(',', wp_parse_id_list($qv['include']));
$this->query_where .= " AND {$wpdb->users}.ID IN ({$ids})";
} elseif (!empty($qv['exclude'])) {
$ids = implode(',', wp_parse_id_list($qv['exclude']));
$this->query_where .= " AND {$wpdb->users}.ID NOT IN ({$ids})";
}
do_action_ref_array('pre_user_query', array(&$this));
}
示例6: get_meta_query_sql
/**
* Get the SQL for the 'meta_query' param in BP_Messages_Thread::get_current_threads_for_user().
*
* We use WP_Meta_Query to do the heavy lifting of parsing the meta_query array
* and creating the necessary SQL clauses.
*
* @since 2.2.0
*
* @param array $meta_query An array of meta_query filters. See the
* documentation for WP_Meta_Query for details.
*
* @return array $sql_array 'join' and 'where' clauses.
*/
public static function get_meta_query_sql($meta_query = array())
{
global $wpdb;
$sql_array = array('join' => '', 'where' => '');
if (!empty($meta_query)) {
$meta_query = new WP_Meta_Query($meta_query);
// WP_Meta_Query expects the table name at
// $wpdb->messagemeta
$wpdb->messagemeta = buddypress()->messages->table_name_meta;
return $meta_query->get_sql('message', 'm', 'id');
}
return $sql_array;
}
示例7: terms_clauses
/**
* Filter `term_clauses` and add support for a `meta_query` argument
*
* @since 0.1.0
*
* @param array $pieces Terms query SQL clauses.
* @param array $taxonomies An array of taxonomies.
* @param array $args An array of terms query arguments.
*
* @return Array of query pieces, maybe modifed
*/
public function terms_clauses($pieces = array(), $taxonomies = array(), $args = array())
{
// Maybe do a meta query
if (!empty($args['meta_query'])) {
// Make doubly sure global database object is prepared
$this->add_termmeta_to_db_object();
// Get the meta query parts
$meta_query = new WP_Meta_Query($args['meta_query']);
$meta_query->parse_query_vars($args);
// Combine pieces & meta-query clauses
if (!empty($meta_query->queries)) {
/**
* It's possible in a future version of WordPress that our
* `term_id` usage might need to be swapped to `term_taxonomy_id`.
*/
$meta_clauses = $meta_query->get_sql('term', 'tt', 'term_id', $taxonomies);
$pieces['join'] .= $meta_clauses['join'];
$pieces['where'] .= $meta_clauses['where'];
}
}
// Return possibly modified pieces array
return $pieces;
}
示例8: hm_add_term_meta_query_support
function hm_add_term_meta_query_support($pieces, $taxonomies, $args)
{
if (empty($args['meta_query'])) {
return $pieces;
}
$meta_query = new WP_Meta_Query($args['meta_query']);
$sql = $meta_query->get_sql('term', 't', 'term_id');
if (!$sql) {
return $pieces;
}
$pieces['join'] .= $sql['join'];
$pieces['where'] .= $sql['where'];
return $pieces;
}
示例9: get_filtered_term_product_counts
/**
* Count products within certain terms, taking the main WP query into consideration.
* @param array $term_ids
* @param string $taxonomy
* @param string $query_type
* @return array
*/
protected function get_filtered_term_product_counts($term_ids, $taxonomy, $query_type)
{
global $wpdb;
$tax_query = WC_Query::get_main_tax_query();
$meta_query = WC_Query::get_main_meta_query();
if ('or' === $query_type) {
foreach ($tax_query as $key => $query) {
if ($taxonomy === $query['taxonomy']) {
unset($tax_query[$key]);
}
}
}
$meta_query = new WP_Meta_Query($meta_query);
$tax_query = new WP_Tax_Query($tax_query);
$meta_query_sql = $meta_query->get_sql('post', $wpdb->posts, 'ID');
$tax_query_sql = $tax_query->get_sql($wpdb->posts, 'ID');
$sql = "\n\t\t\tSELECT COUNT( {$wpdb->posts}.ID ) as term_count, term_count_relationships.term_taxonomy_id as term_count_id FROM {$wpdb->posts}\n\t\t\tINNER JOIN {$wpdb->term_relationships} AS term_count_relationships ON ({$wpdb->posts}.ID = term_count_relationships.object_id)\n\t\t\t" . $tax_query_sql['join'] . $meta_query_sql['join'] . "\n\t\t\tWHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish'\n\t\t\t" . $tax_query_sql['where'] . $meta_query_sql['where'] . "\n\t\t\tAND term_count_relationships.term_taxonomy_id IN (" . implode(',', array_map('absint', $term_ids)) . ")\n\t\t\tGROUP BY term_count_relationships.term_taxonomy_id;\n\t\t";
$results = $wpdb->get_results($sql);
return wp_list_pluck($results, 'term_count', 'term_count_id');
}
示例10: array
function test_empty_compare()
{
global $wpdb;
$query = new WP_Meta_Query(array('relation' => 'OR', array('key' => 'exclude', 'compare' => ''), array('key' => 'exclude', 'compare' => '!=', 'value' => '1')));
$sql = $query->get_sql('post', $wpdb->posts, 'ID', $this);
$this->assertContains("{$wpdb->postmeta}.meta_key = 'exclude'\nOR", $sql['where']);
$this->assertNotContains("{$wpdb->postmeta}.post_id IS NULL", $sql['where']);
}
示例11:
/**
* Inject the meta_query SQL in SearchWP's WHERE
*
* @since 2.6
*
* @param $sql
* @param $engine
*
* @return string
*/
function meta_where($sql, $engine)
{
if ($engine != $this->engine || empty($this->meta_query) || !is_array($this->meta_query)) {
return $sql;
}
global $wpdb;
$meta_query = new WP_Meta_Query($this->meta_query);
$mq_sql = $meta_query->get_sql('post', $wpdb->posts, 'ID', null);
return $sql . $mq_sql['where'];
}
示例12: get_filtered_product_count
/**
* Count products after other filters have occured by adjusting the main query.
* @param int $rating
* @return int
*/
protected function get_filtered_product_count($rating)
{
global $wpdb;
$tax_query = WC_Query::get_main_tax_query();
$meta_query = WC_Query::get_main_meta_query();
// Unset current rating filter
foreach ($meta_query as $key => $query) {
if (!empty($query['rating_filter'])) {
unset($meta_query[$key]);
}
}
// Set new rating filter
$meta_query[] = array('key' => '_wc_average_rating', 'value' => $rating, 'compare' => '>=', 'type' => 'DECIMAL', 'rating_filter' => true);
$meta_query = new WP_Meta_Query($meta_query);
$tax_query = new WP_Tax_Query($tax_query);
$meta_query_sql = $meta_query->get_sql('post', $wpdb->posts, 'ID');
$tax_query_sql = $tax_query->get_sql($wpdb->posts, 'ID');
$sql = "SELECT COUNT( {$wpdb->posts}.ID ) FROM {$wpdb->posts} ";
$sql .= $tax_query_sql['join'] . $meta_query_sql['join'];
$sql .= " WHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish' ";
$sql .= $tax_query_sql['where'] . $meta_query_sql['where'];
return absint($wpdb->get_var($sql));
}
示例13: array
/**
* @ticket 22967
*/
function test_null_value_sql() {
global $wpdb;
$query = new WP_Meta_Query( array(
array( 'key' => 'abc', 'value' => null, 'compare' => '=' )
) );
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
$this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) );
}
示例14: get_meta_query_sql
/**
* Get the SQL for the 'meta_query' param in BP_Activity_Activity::get()
*
* We use WP_Meta_Query to do the heavy lifting of parsing the
* meta_query array and creating the necessary SQL clauses. However,
* since BP_Activity_Activity::get() builds its SQL differently than
* WP_Query, we have to alter the return value (stripping the leading
* AND keyword from the 'where' clause).
*
* @since 1.8.0
*
* @param array $meta_query An array of meta_query filters. See the
* documentation for {@link WP_Meta_Query} for details.
* @return array $sql_array 'join' and 'where' clauses.
*/
protected static function get_meta_query_sql($meta_query = array())
{
global $wpdb;
$sql_array = array('join' => '', 'where' => '');
if (!empty($meta_query)) {
$groups_meta_query = new WP_Meta_Query($meta_query);
// WP_Meta_Query expects the table name at
// $wpdb->group.
$wpdb->groupmeta = buddypress()->groups->table_name_groupmeta;
$meta_sql = $groups_meta_query->get_sql('group', 'g', 'id');
// BP_Groups_Group::get uses the comma syntax for table
// joins, which means that we have to do some regex to
// convert the INNER JOIN and move the ON clause to a
// WHERE condition
//
// @todo It may be better in the long run to refactor
// the more general query syntax to accord better with
// BP/WP convention.
preg_match_all('/JOIN (.+?) ON/', $meta_sql['join'], $matches_a);
preg_match_all('/ON \\((.+?)\\)/', $meta_sql['join'], $matches_b);
if (!empty($matches_a[1]) && !empty($matches_b[1])) {
$sql_array['join'] = implode(',', $matches_a[1]) . ', ';
$sql_array['where'] = $meta_sql['where'] . ' AND ' . implode(' AND ', $matches_b[1]);
}
}
return $sql_array;
}
示例15: get_filtered_term_product_counts
/**
* Count products within certain terms, taking the main WP query into consideration.
*
* @param array $term_ids
* @param string $taxonomy
* @param string $query_type
* @return array
*/
protected function get_filtered_term_product_counts($term_ids, $taxonomy, $query_type)
{
global $wpdb;
$tax_query = WC_Query::get_main_tax_query();
$meta_query = WC_Query::get_main_meta_query();
if ('or' === $query_type) {
foreach ($tax_query as $key => $query) {
if (is_array($query) && $taxonomy === $query['taxonomy']) {
unset($tax_query[$key]);
}
}
}
$meta_query = new WP_Meta_Query($meta_query);
$tax_query = new WP_Tax_Query($tax_query);
$meta_query_sql = $meta_query->get_sql('post', $wpdb->posts, 'ID');
$tax_query_sql = $tax_query->get_sql($wpdb->posts, 'ID');
// Generate query
$query = array();
$query['select'] = "SELECT COUNT( DISTINCT {$wpdb->posts}.ID ) as term_count, terms.term_id as term_count_id";
$query['from'] = "FROM {$wpdb->posts}";
$query['join'] = "\n\t\t\tINNER JOIN {$wpdb->term_relationships} AS term_relationships ON {$wpdb->posts}.ID = term_relationships.object_id\n\t\t\tINNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy USING( term_taxonomy_id )\n\t\t\tINNER JOIN {$wpdb->terms} AS terms USING( term_id )\n\t\t\t" . $tax_query_sql['join'] . $meta_query_sql['join'];
$query['where'] = "\n\t\t\tWHERE {$wpdb->posts}.post_type IN ( 'product' )\n\t\t\tAND {$wpdb->posts}.post_status = 'publish'\n\t\t\t" . $tax_query_sql['where'] . $meta_query_sql['where'] . "\n\t\t\tAND terms.term_id IN (" . implode(',', array_map('absint', $term_ids)) . ")\n\t\t";
if ($search = WC_Query::get_main_search_query_sql()) {
$query['where'] .= ' AND ' . $search;
}
$query['group_by'] = "GROUP BY terms.term_id";
$query = apply_filters('woocommerce_get_filtered_term_product_counts_query', $query);
$query = implode(' ', $query);
$results = $wpdb->get_results($query);
return wp_list_pluck($results, 'term_count', 'term_count_id');
}