本文整理汇总了PHP中WP_Meta_Query类的典型用法代码示例。如果您正苦于以下问题:PHP WP_Meta_Query类的具体用法?PHP WP_Meta_Query怎么用?PHP WP_Meta_Query使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WP_Meta_Query类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_sql
function get_sql()
{
global $wpdb;
if (empty($this->queries)) {
return array('join' => '', 'where' => '');
}
$context_table = MainWP_WP_Stream_DB::$table_context;
$main_table = MainWP_WP_Stream_DB::$table;
$meta_id_column = 'meta_id';
$join = array();
$where = array();
$queries = $this->queries;
$meta_query = new WP_Meta_Query();
foreach ($queries as $i => $query) {
foreach ($query as $key => $args) {
$type = $meta_query->get_cast_for_type(isset($args['type']) ? $args['type'] : '');
$value = isset($args['value']) ? $args['value'] : null;
// Allow 'context' => array('val1', 'val2') as well
if (is_null($value)) {
$args = array('value' => $args);
$value = $args['value'];
}
if (isset($args['compare'])) {
$compare = strtoupper($args['compare']);
} else {
$compare = is_array($value) ? 'IN' : '=';
}
$operators = array('=', '!=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'REGEXP', 'NOT REGEXP', 'RLIKE');
if (!in_array($compare, $operators)) {
$compare = '=';
}
if ('IN' === substr($compare, -2)) {
if (!is_array($value)) {
$value = preg_split('/[,\\s]+/', $value);
}
$compare_string = '(' . substr(str_repeat(',%s', count($value)), 1) . ')';
} elseif ('LIKE' === substr($compare, -4)) {
$value = '%' . like_escape($value) . '%';
$compare_string = '%s';
} else {
$compare_string = '%s';
}
if (!empty($where[$i])) {
$where[$i] .= ' AND ';
} else {
$where[$i] = '';
}
$where[$i] = ' (' . $where[$i] . $wpdb->prepare("CAST({$context_table}.{$key} AS {$type}) {$compare} {$compare_string})", $value);
}
}
$where = array_filter($where);
if (empty($where)) {
$where = '';
} else {
$where = ' AND (' . implode("\n{$this->relation} ", $where) . ' )';
}
$join = implode("\n", $join);
return apply_filters_ref_array('get_context_sql', array(compact('join', 'where'), $this->queries));
}
示例2: test_empty_value_sql
/**
* @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: wpToLabel
/**
* Convert WP_Query query_var value into a human readable label
*
* @param array $query_vars WP_Query query vars.
* @return string the label based on the given vars.
*/
public function wpToLabel($query_vars)
{
$meta_query = new WP_Meta_Query();
$meta_query->parse_query_vars($query_vars);
$label = '';
if (count($meta_query->queries) > 0) {
$expressionSet = new Lift_Expression_Set(strtolower($meta_query->relation));
foreach ($meta_query->queries as $subquery) {
if ($subquery['key'] == $this->meta_key) {
$label = (string) $subquery->value;
}
}
}
return $label;
}
示例4: 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;
}
示例5: 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;
}
示例6: 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));
示例7: test_get_cast_for_type
/**
* @ticket 23033
*/
function test_get_cast_for_type() {
$query = new WP_Meta_Query();
$this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'CHAR' ) );
$this->assertEquals( 'DATE', $query->get_cast_for_type( 'DATE' ) );
$this->assertEquals( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) );
$this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) );
$this->assertEquals( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) );
$this->assertEquals( 'TIME', $query->get_cast_for_type( 'TIME' ) );
$this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) );
$this->assertEquals( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) );
$this->assertEquals( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
$this->assertEquals( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) );
$this->assertEquals( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) );
$this->assertEquals( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) );
$this->assertEquals( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
$this->assertEquals( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
}
示例8: meta_where
/**
* 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'];
}
示例9: get_meta_query_sql
/**
* Get the SQL for the 'meta_query' param in BP_Notifications_Notification::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_Notifications_Notification::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 2.3.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())
{
// Default array keys & empty values.
$sql_array = array('join' => '', 'where' => '');
// Bail if no meta query.
if (empty($meta_query)) {
return $sql_array;
}
// WP_Meta_Query expects the table name at $wpdb->notificationmeta.
$GLOBALS['wpdb']->notificationmeta = buddypress()->notifications->table_name_meta;
$n_meta_query = new WP_Meta_Query($meta_query);
$meta_sql = $n_meta_query->get_sql('notification', 'n', 'id');
// Strip the leading AND - it's handled in get().
$sql_array['where'] = preg_replace('/^\\sAND/', '', $meta_sql['where']);
$sql_array['join'] = $meta_sql['join'];
return $sql_array;
}
示例10: __construct
/**
* Initialize the class
* @param string|array $args
*/
public function __construct($args = '')
{
global $wpdb;
ap_wpdb_tables();
$this->per_page = 20;
// Grab the current page number and set to 1 if no page number is set.
$this->paged = isset($args['paged']) ? (int) $args['paged'] : 1;
$this->offset = $this->per_page * ($this->paged - 1);
$this->args = wp_parse_args($args, array('number' => $this->per_page, 'offset' => $this->offset, 'orderby' => 'date', 'order' => 'DESC', 'notification' => false));
// Process meta query arguments.
if (isset($this->args['meta_query'])) {
$meta_query = new WP_Meta_Query();
$meta_query->parse_query_vars($this->args['meta_query']);
$this->meta_query_sql = $meta_query->get_sql('ap_activity', $wpdb->ap_activity, 'id', null);
}
$this->parse_query();
$this->total_activity_count = $wpdb->get_var(apply_filters('ap_found_activity_query', 'SELECT FOUND_ROWS()', $this));
}
示例11: test_has_or_relation_should_return_true_for_nested_or
/**
* @group 32592
*/
public function test_has_or_relation_should_return_true_for_nested_or()
{
$q = new WP_Meta_Query(array('relation' => 'AND', array('key' => 'foo', 'value' => 'bar'), array('relation' => 'OR', array('key' => 'foo1', 'value' => 'bar'), array('key' => 'foo2', 'value' => 'bar'))));
$this->assertTrue($q->has_or_relation());
}
示例12: 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');
}
示例13: 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;
}
示例14: test_empty_compare
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']);
}
示例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 ($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');
}