Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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'
            );
    }
}
share|improve this question
    
Looks OK. Are you sure the query is giving the search logic you need? Look at the actual generated SQL. –  s_ha_dum Jan 19 at 15:58
    
Is that correct? Only you know if the query fits your requirements. –  s_ha_dum Jan 19 at 18:12
1  
The SQL looks correct. So I'm guessing you don't have any posts with casino = Test Casino. –  Otto Jan 19 at 18:24
    
Meta query will pull those posts who have the related meta key. You can check the database for the meta key-value combination for the post. If not present, then you can add some values there and then try executing your query. –  Chittaranjan Jan 19 at 18:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.