0

I've following query -

$query = array(
    'post_type' => 'accessory',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'accessory-bike',
            'value' => array("33"),
            'compare' => 'IN'
        )
    )
);

And now I've this record in database -

enter image description here

520 is the ID of post having accessory post type. It still returns 0 results. I can't figure where the hell did I do wrong ? Stupid wordpress.

This generates following query -

SELECT   wp_posts.* 
        FROM wp_posts  
        INNER JOIN wp_postmeta 
        ON ( wp_posts.ID = wp_postmeta.post_id ) 
        WHERE 1=1  
        AND wp_posts.post_type = 'accessory' 
        AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private') 
        AND ( ( wp_postmeta.meta_key = 'accessory-bike' AND CAST(wp_postmeta.meta_value AS SIGNED) IN ('33') ) )
        GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC

which if you see carefully is completely accurate.

6
  • 3
    for your query to succeed, the value must be 33, and as you can see in your screenshot, the value is a serialized array, not 33.
    – Milo
    Mar 25, 2015 at 20:25
  • @Milo I'm using that ACF plugin and it stores meta values like that. There's no way I can now search by these meta values. :(
    – Gimali
    Mar 25, 2015 at 20:41
  • 1
    Storing data that needs to be searchable in a serialized array is a bad idea, but given the choice of helper plugins you are using you are stuck with it. Perhaps: wordpress.stackexchange.com/a/115837/21376
    – s_ha_dum
    Mar 25, 2015 at 21:23
  • So re wordpress.stackexchange.com/a/115837/21376 if you do 'compare' => 'LIKE' and 'value' => '"33"' it should work...
    – bonger
    Mar 25, 2015 at 21:37
  • You'll need to unserialize the meta value, much like they do here: wordpress.stackexchange.com/questions/18463/…
    – leromt
    Mar 25, 2015 at 22:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.