• Hi Steve

    Me again. Any idea if the values stored as an array (e.g. entered via a checkbox group) behave any differently in this plugin? If not, then it’s possibly my WordPress knowledge that’s lacking but either way, any idea how to use array values in a meta_query? i.e. as part of the args for a get_posts or WP_Query?
    E.g. Here’s what I’m working with (the $args list is longer so have just included the relevant part):

    $args = array(

    ‘meta_query’ => array(
    array(
    ‘key’ => ‘_slt_client_by_year’,
    ‘value’ => ‘2010’, // the values saved are 2010, 2011 etc
    ‘compare’ => ‘IN’,
    )
    )

    Essentially I’m trying to find out whether the year e.g. 2010 is one of the values saved in the custom field _slt_client_by_year

    The meta_query certainly works with non-array values (by using compare=>”=”) but so far no success searching for a value within an array.

    Hope I’ve explained that clearly enough.

    Thanks for your help

    Tim

    https://wordpress.org/plugins/developers-custom-fields/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter timboreader

    (@timboreader)

    I should add: I think it works with “LIKE”. Perhaps that’s the solution but was wondering whether “IN” is more correct and whether it should be working with that.
    If not, and ‘like’ is fine, then fine! 🙂

    Plugin Author Steve Taylor

    (@gyrus)

    Hi!

    Small point first, best to use slt_cf_field_key( 'client_by_year' ) instead of just _slt_client_by_year. The latter will work, but the former is more future-proof (don’t know why the prefix might change, but who knows?)

    If I understand your question, you might be needing to look at the field parameter single. From the docs:

    For fields that have multiple values (e.g. multiple checkboxes and selects), setting this to false will create multiple fields in the postmeta table, instead of storing a serialized array of values in one field. This allows for more flexible use of the meta_query parameter for queries. When you grab this kind of field with all other fields using slt_cf_all_field_values, remember to pass this field’s key in the $multiple_fields array.

    Is that what you’re getting at? If the field is set so that multiple entries in the meta table for the same post, same field are created (wp_postmeta is designed to work that way if necessary), then you can just use ‘=’ as the operator.

    Thread Starter timboreader

    (@timboreader)

    Thanks Steve, I’m pretty sure you understand the question 100%. Both ways seem to work but yours seems that much closer to error-proof so am going to go with that.
    More I work with it, more I think this is one of the best plugins out there.
    Donation coming your way shortly.
    Thanks
    Tim

    Plugin Author Steve Taylor

    (@gyrus)

    Many thanks Tim! I think the plugin needs a complete overhaul, but it doesn’t seem to be worth doing until the core Metadata API gets done. But that seems to be taking a while 🙁

    Plugin Author Steve Taylor

    (@gyrus)

    Many thanks Tim! I think the plugin needs a complete overhaul, but it doesn’t seem to be worth doing until the core Metadata API is released. But that seems to be taking a while 🙁

    Thread Starter timboreader

    (@timboreader)

    Just checking, the donate link from the plugin page goes to a site called Corporate Watch – is that where you want donations to go?
    T

    Plugin Author Steve Taylor

    (@gyrus)

    Yes indeed, my nominated non-profit.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Meta query with arrays’ is closed to new replies.