black friday sale

Custom Fields
Re-Envisioned
Support General best approach when it comes to query by custom field

  • Creator
    Topic
  • #22905
    Brian
    Participant

    Hi Meta Box,
    I’m still a little lost on the best approach when it comes to querying by custom field. I know you recommend we don’t querying by custom field but does that still apply when using the plugin FacetWP?

    Here is my problem: I’ve created a custom post type for “Events” and each event can have multiple dates. I’m using the date field in a “Group” so that I can add additional dates. I realize that since it’s now in a group that it’s serialized.

    I’m trying to create an events grid similar to this example (notice the first event has multiple dates associated but only shows up one time): https://share.getcloudapp.com/E0u455kE

    And then on the actual “Events” page I would list out each available date like this example: https://share.getcloudapp.com/yAuZ7XGO

    That being said what would you advise on best approach to query grid by date field?

    Thanks in advance!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Author
    Replies
  • #22910
    Long Nguyen
    Moderator

    Hi Brian,

    Query by subfield’s value in a group is very difficult because the data saved is a serialized array. I recommend using the cloneable field and field setting clone_as_multiple to get multiple date and query by its value.

    Follow this article for more information https://metabox.io/introducing-clone-as-multiple-feature/

    #22911
    Brian
    Participant

    Hi Long,
    Thanks for that info as I wasn’t aware of the ‘clone_as_multiple’ option. Will this work if the fields are in a MB Custom Table?

    Thanks,
    Brian

    #22920
    Long Nguyen
    Moderator

    Hi Brian,

    The field setting clone_as_multiple does not work with the custom table. It saves a serialized array in one cell like the group.
    https://docs.metabox.io/extensions/mb-custom-table/#group-fields

    #22937
    Brian
    Participant

    Hi Long,
    So I just realized that my date fields are within a group and the group is cloneable. So does that mean the ‘clone_as_multiple’ feature will not work because it’s in a group? I have a few other fields that I needed to associate to the date fields that’s why I had then in a group.

    Thanks,
    Brian

    #22947
    Long Nguyen
    Moderator

    Hi,

    The setting clone_as_multiple works with all cloneable fields in the default WordPress table (postmeta). If this setting is set to false, the cloneable groups will be saved in one row like

    {serialized array1, serialized array2 ...}
    

    If true, it will be saved in more rows like

    {serialized array1}
    {serialized array2}
    ...
    

    And please notice that the group is used for storing data, not for querying.

    #22956
    Brian
    Participant

    Hi Logn,
    My query & order by custom date field is not working.

    Since my date fields are within a group field, and it’s the group field that’s cloneable what would be the key and value so that my query is ordered by my the custom date field?

    My group id is: ct_event_date_group
    And my event date field id is: ct_event_date

    Here is what I have but it’s not working:

    // Events Query
    $events_query = new WP_Query(array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'meta_key' => 'ct_event_date_group',
    'orderby' => 'meta_value',
    'order' => 'DESC'
    ));
    #22959
    Long Nguyen
    Moderator

    Hi Brian,

    As I said, the group is used for storing data (sub-fields value) not for querying by sub-fields value because the value of the group saved in the database is a serialized array. See more in https://metabox.io/create-group-of-custom-fields-with-meta-box-group/#how-does-the-meta-box-group-save-data.

    This topic from StackOverFlow might help you to achieve your goal https://stackoverflow.com/questions/26364378/wp-query-when-meta-value-saved-as-serialized-array.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.