Support

Account

Home Forums General Issues WP_Query with meta_query on a subfield within a group field

Solved

WP_Query with meta_query on a subfield within a group field

  • I’m trying to call WP_Query to query posts of a custom post type on an archive page with using meta_query of a subfield within aa ACF group field.

    The setup is as follows:
    • Custom post type ‘images’
    • Custom post type ‘people’

    On the image edit page I have:
    • ACF group field ‘authors’
    • ACF post_object multiselect ‘people’ inside ‘authors’ group

    I want to be able to query images authored by a specific person by the person id, I do not want to change the field structure because this has been design for other pages and archives and needs to remain the same.

    … in functions.php

    function my_posts_where( $where ) {
        global $wpdb;
        $where = str_replace(
            "meta_key = 'authors_",
            "meta_key LIKE 'authors_",
            $wpdb->remove_placeholder_escape( $where )
        );
        return $where;
    }
    add_filter( 'posts_where', 'my_posts_where' );

    … in archive-images.php

    $author = 85; // This will eventuall be pulled from get_query_var('author')
    $images_query = new WP_Query([
        'suppress_filters' => FALSE,
        'posts_per_page' => -1,
        'post_type' => 'images',
        'meta_query' => [
            //'relation' => 'OR',
            //'relation' => 'AND',
            [
                'key' => 'authors_%_people',
                'compare' => 'LIKE',
                //'value' => '"'.$author.'"',
                'value' => array( '"'.$author.'"' ),
            ],
        ],
    ]);

    I have pieced this together from various sources and I’m assuming the group field can be treated like a single repeater… but it simply is not working. I’ve done tests where the people post_object select is outside the group field and it works ( allbeit with a warning error due to wrapping value in array ).

    So my issue lies with querying a subfield inside a group field. Please help if you can, I’m going mad with it.

  • Hey Julius,

    I’ve read a bit more about group fields and the key doesn’t act the same as a repeater.

    $author = 85; // This will eventuall be pulled from get_query_var('author')
    $images_query = new WP_Query([
        'suppress_filters' => FALSE,
        'posts_per_page' => -1,
        'post_type' => 'images',
        'meta_query' => [
            //'relation' => 'OR',
            //'relation' => 'AND',
            [
                'key' => 'authors_people',
                'compare' => 'LIKE',
                'value' => '"'.$author.'"'
            ],
        ],
    ]);

    In the second paragraph of the overview for group it says the database saves the result as group_field so in your case it is authors_people. As the people field is a multi select and holding an array of people IDs (I would have thought) then the LIKE comparator should be fine.

    Try the above and let me know what you get.

  • Yes! I just looked that up a few minutes ago after re-reading your last post on the other thread.. and tried exactly this. This was it! Thanks!

    Any idea why I get a warning error if I have the value in an array?
    'value' => array( '"'.$author.'"' ),

    I seem to need this if the post_object is not in a group.
    ( I know mine is but for future use I’d like to crack it all round )

  • If didn’t have your people post_object in a group then you’d just change the key to become ‘people’, is your comparator still LIKE?

  • Sorry for delay, so I was trying two things at once..

    • Meta_query without post_object field in a group
    • Meta_query on a post_object field that is not multi-select

    The first is obvious as you pointed out but is dependant on the second
    Am I correct in thinking that for the second I would need to omit the double quotes?
    'value' => $instrument,
    ..Instead of..
    'value' => '"'.$instrument.'"',

    What about compare? Should I use LIKE or IN for single post_object?

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘WP_Query with meta_query on a subfield within a group field’ is closed to new replies.