Sorting loops by a subfield in a nested group

Support MB Views Sorting loops by a subfield in a nested groupResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #33022
    AMXAMX
    Participant

    Hi,

    I would like to sort loop items by the value of a number field in a nested group (2 levels down).
    I can sort by top-level fields without problems but not by nested subfields.

    Something like this obviously doesn't work:

    {% set args = 
        { 
        post_type: 'my-cpt', 
        posts_per_page: -1, 
        meta_key: 'my_group.my_group_2.my_subfield',
        orderby: 'meta_value', 
        order: 'ASC', 
        } 
    %}
    {% set posts =  mb.get_posts(args) %}

    Using only 'my_subfield' doesn't work either.

    I also tried to sort using Twig's "|sort":

    {% set sortorder = '' %}
    {% for post in posts|sort((a, b) => a.sortorder <=> b.sortorder) %}    
    {% set sortorder = post.my_group.my_group2.my_subfield %}

    But that won't work, either.
    How can I sort by a nested subfield?

    Greetings,
    Tom

    #33023
    AMXAMX
    Participant

    FacetWP has now sorting controls, too. But it doesn't see nested subfields, only top-level fields/groups are visible in FacetWP... Can subfields inside groups work with FacetWP?

    #33041
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The group is used for storing data (sub-fields value) not for querying by sub-fields (sort or filter) 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.

    You should use the top field for sorting or filtering (FacetWP).

    #33106
    AMXAMX
    Participant

    Thank you, Long.

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