Wp_Query meta_query not working - wordpress

I'm trying to do the equivalent of this in meta_query but so far no success.
WHERE tm_url_1="" AND tm_url_2="" AND (tm_url_3="" OR tm_embed_code=!="") AND (tm_url_3!="" OR tm_embed_code=="")
Here the WP_Query. Once i add the OR clause nothing is returned.
$args = array(
'post_status' => $post_status,
'posts_per_page' => $posts_count,
'ignore_sticky_posts' => 1,
'post_type' => $post_type,
'order_by' => $order_by,
'order' => $order,
"meta_query" => array(
array('relation' => 'AND',
array(
'key' => 'tm_url_1',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_url_2',
'value' => '',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '='
)
)
)
)
);
Nothing is returned. Where did i go wrong in the meta_query?

you should remove the first "array" in the media_query parameter:
"meta_query" =>
array(
'relation' => 'AND',
array(
'key' => 'tm_url_1',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_url_2',
'value' => '',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '='
)
)
)
see: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

Here is the correct way to add the meta_query for the conditions you want.
$args = array(
'post_status' => $post_status,
'posts_per_page' => $posts_count,
'ignore_sticky_posts' => 1,
'post_type' => $post_type,
'order_by' => $order_by,
'order' => $order,
'meta_query' => array(
array(
'key' => 'tm_url_1',
'value' => '',
) ,
array(
'key' => 'tm_url_2',
'value' => '',
) ,
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
) ,
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
) ,
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
) ,
array(
'key' => 'tm_embed_code',
'value' => '',
)
)
)
);
Note that I have removed the default values for compare in some arrays assuming it is = as in your condition. The reference for this can be seen here.

Related

WP several meta_query matches, and double counted

I set following meta_query.
I like to add one more condition.
My problem is that the article which matches both meta_query conditions are double counted by count(get_posts on the end of line.
Is it possible to add condition like this? If both 1bb and 1aa matches, count it as if only 1bb matches.
<?php
$args = array(
'category_name' => $cat,
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'or',
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
echo count( get_posts( $args ) );
?>
Thank you.
The following is additional code that I tried to prevent from double counting.
Its too long time to load, and can not use it.
'meta_query' => array(
'relation' => 'or',
array(
'relation' => 'AND',
array(
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1bb',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'EXISTS',
),
array(
'key' => '1bb',
'compare' => 'EXISTS',
),
),
),
),

get post base on latest date without comparision

I have custom post type having meta value include last update date.
I have to get post id in which meta value having latest date without compare other date.
below is my wp query to get post id but it is not working.
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'meta_value_num' => 'ASC','ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
If you are using WP 4.2+, you may try the following:
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'post_last_update_date' => 'ASC', 'ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
Some sources:
General WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
4.0+ orderby array: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/
4.2+ orderby meta data: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

WordPress Meta Query with Multi Value Arrays

I have the following query that I would like to pass an array of values to:
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
array(
'key' => '_customer_names',
'value' => $customer_names,
'compare' => '='
),
array(
'key' => '_customer_dates',
'value' => $customer_dates,
'compare' => 'LIKE'
)
)
)
);
For example, I would like to pass like this:
$customer_names = array('John','Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
The query will also need to handle the possibility that all customers could have data for each date. In pseudo SQL, my best guess would be:
SELECT * FROM WORDPRESS_POSTS WHERE _customer_names = (John OR Tom OR Simon) AND customer_dates = (20161225 OR 20161226 OR 20161227)
However, at the moment, even when I remove the date restriction, I can't find any posts. Hence, I wanted to confirm my logic is correct.
Try like this:
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => '_customer_names',
'value' => 'John',
'compare' => '='
),
array(
'key' => '_customer_names',
'value' => 'Tom',
'compare' => '='
),
array(
'key' => '_customer_names',
'value' => 'Simon',
'compare' => '='
)
),
array(
'relation' => 'OR',
array(
'key' => '_customer_dates',
'value' => '20161225',
'compare' => '='
),
array(
'key' => '_customer_dates',
'value' => '20161225',
'compare' => '='
),
array(
'key' => '_customer_dates',
'value' => '20161225',
'compare' => '='
)
),
)
)
);
if you want to use array in values you need to use compare with IN clause.
OR if you want to match with exact values you can try #Ravendra Patel's solution.
Also use WP_QUERY because it comes with main query and provide more help to identify the issue
$customer_names = array('John', 'Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
$args = array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
array(
'key' => '_customer_names',
'value' => $customer_names,
'compare' => 'IN'
),
array(
'key' => '_customer_dates',
'value' => $customer_dates,
'compare' => 'IN'
)
)
);
$query = new WP_QUERY($args);
echo 'SQL: '.$query->request.'<br>'; // your query against args
try like this:
$customer_names = array('John', 'Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
$cm_metaq = array();
foreach($customer_names as $cm){
$cm_metaq[] = array('key' => '_customer_names', 'value' => $cm, 'compare' => '=');
}
$cd_metaq = array();
foreach($customer_dates as $cd){
$cd_metaq[] = array('key' => '_customer_dates', 'value' =>$cd, 'compare' => '=');
}
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
$cm_metaq
),
array(
'relation' => 'OR',
$cd_metaq
),
)
)
);

Query with meta key is serialized array

I've made a custom query containing a meta query but some fields are serialized arrays. Is there a way to get the value from that array? Everything I've tried so far won't work.
$query_args = array(
'post_type' => 'yacht',
'_meta_or_tax' => true,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'manufacturers',
'field' => 'id',
'terms' => $_GET['manufacturer']
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_GET['cat'],
)
),
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'yachts_length',
'value' => array($_GET['min_length'], $_GET['max_length']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_price',
'value' => array($_GET['min_price'], $_GET['max_price']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_year',
'value' => array($_GET['min_year'], $_GET['max_year']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_fuel',
'value' => $_GET['fuel'],
'compare' => 'LIKE',
),
array(
'key' => 'yachts_cabins',
'value' => $_GET['cabins'],
'compare' => 'LIKE',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_engine_type',
'value' => $_GET['engine_type'],
'compare' => 'LIKE',
),
array(
'key' => 'yachts_en',
'value' => $_GET['engines'],
'compare' => 'LIKE',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_drive',
'value' => $_GET['drive_type'],
'compare' => 'LIKE',
),
)
);
$yacht_query = new WP_Query( $query_args );
yachts_cabins, yachts_fuel, yachts_engine_type, yachts_en, yachts_type are serialized arrays.

Meta Queries - should nesting work after WP 4.1?

I have a rather huge meta_query here and it hangs at execution: browser just won't load the page, it goes on loading ...
As far as I know this should work after WP 4.1 (I have it). Also tried updating to 4.2 to no avail.
Am I doing something terribly wrong here? Thanks for any info.
$meta_query = array(
'relation' => 'AND',
// IF $level GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_level',
'value' => $level,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level2',
'value' => $level,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level3',
'value' => $level,
'compare' => 'IN'
),
),
),
// IF $year GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_year',
'value' => $year,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year2',
'value' => $year,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year3',
'value' => $year,
'compare' => 'IN'
),
),
),
);
$args = array(
'post_type' => 'course',
'posts_per_page' => -1, // get all posts
'orderby' => array('meta_value_num'=>'ASC','menu_order'=>'ASC'), // order by given meta value AND menu_order
'meta_key' => $meta_key_orderby,
'order' => 'ASC',
'meta_query' => $meta_query,
);

Resources