I am trying to get list of posts filtered by fields I added using the advance custom fields plugin. I have more fields that I want to use but it doesn’t work as I want it to.
Here is what the query looks like:
"query":{
"posts_per_page":"10",
"orderby":"meta_value_num",
"meta_key":"site_return_rate",
"meta_query":[
{
"key":"casino",
"value":"Test Casino",
"compare":"="
}
],
"post_type":"slot",
"post_status":"publish"
},
Without the meta_query
the results are fine, I tried with like
instead of =
but it still isn’t working
=== Update ===
It’s an array in array, the above example is what wp_query
returns, here is the code that creates the meta_query
:
if(!empty($_GET['casino_name']) ||!empty($_GET['slot_type'])){
$args['meta_query']=array();
if(!empty($_GET['casino_name'])){
$args['meta_query'][]=array(
'key'=>'casino',
'value'=>$_GET['casino_name'],
'compare'=>'='
);
}
if(!empty($_GET['slot_type'])){
$args['meta_query'][]=array(
'key'=>'slot_type',
'value'=>$_GET['slot_type'],
'compare'=>'='
);
}
}
=== Update 2 ===
Here is the request query that wp_query
returns:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)nINNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) WHERE 1=1 AND wp_posts.post_type = 'slot' AND (wp_posts.post_status = 'publish') AND (wp_postmeta.meta_key = 'site_return_rate'nAND (mt1.meta_key = 'casino' AND CAST(mt1.meta_value AS CHAR) = 'Test Casino') ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 DESC LIMIT 0, 10
=== update 3 ===
After some time and some question in comments(thx) i remembered that the fields are category type and the value of filed is actualy the term id not the name, it works now but i am just wandering if there is a better way then what i do :
if(!empty($_GET['casino_name']) ||!empty($_GET['slot_type'])){
$args['meta_query']=array();
if(!empty($_GET['casino_name'])){
$temp = $wpdb->get_results("select term_id from wp_terms where name like '%".mysql_real_escape_string($_GET['casino_name'])."%'");
$casino_list=array();
foreach ($temp as $key => $value) {
$casino_list[]=$value->term_id;
}
$args['meta_query'][]=array(
'key'=>'casino',
'value'=>$casino_list,
'compare'=>'IN'
);
}
if(!empty($_GET['slot_type'])){
$temp = $wpdb->get_results("select term_id from wp_terms where name like '%".mysql_real_escape_string($_GET['slot_type'])."%'");
$slot_type_list=array();
foreach ($temp as $key => $value) {
$slot_type_list[]=$value->term_id;
}
$args['meta_query'][]=array(
'key'=>'slot_type',
'value'=>$slot_type_list,
'compare'=>'IN'
);
}
}