Support

Account

Home Forums General Issues Meta query for post object?

Solved

Meta query for post object?

  • That throws an error …
    Parse error: syntax error, unexpected '.85' (T_DNUMBER), expecting ']
    ( even after I added the missing comma after ‘LIKE’ ) What’s the ‘”‘.85.'” for?

  • Note :
    Actually my authors filed is not a repeater; its a group field but it’s basically the same as a repeater without the repeats.

  • Oh Sorry my mistake, can you replace it with :

    '"85"'

  • I cannot see where I’m going wrong.. Perhaps it’s relation in the meta_query ? Here is what I have now.

    In my function.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' );

    And on my 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.'"',
            ],
        ],
    ]);

    What am I doing wrong?

  • Let me know if you have any ideas, I’m going mad.

  • Ok, so I’ve narrowed it down to being a problem with my querying subfields inside a group field because I’ve tried the following:

    • created a test_people post_object field outside of the authors group
    • Keeping the return format as post object
    • Wrap the value in an array

    I’m able to get results with this code:

    $author = 85;
    $images_query = new WP_Query( array(
        'post_type' => 'images',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'test_people',
                'value' => array( '"'.$author.'"' ),
                'compare' => 'LIKE',
            ),
        ),
    ));

    *wrapping the value in an array causes another problem but I want to solve the first:
    ( Warning: trim() expects parameter 1 to be string )

    The point it is, I’m not finding a way to query subfields with the function and this syntax:
    'key' => 'authors_%_people'

    Am I wrong in thinking groups can be treated like repeaters?

  • I do not use groups all that often. Although reading here the database saves the key differently. So you need to look in authors_people

    You can also remove the filter you added just now.

    Because your people is a multi select then I believe you need to use LIKE

  • @meandhim, thanks for your help, still not cracked it. I don’t know how to ‘look in authors_people’.. Do you mean look at the mySQL database? This is a bit over my head.

    I have moved my issue to a new forum thread, please come over there to chip in if you have any thoughts on this and to continue this. Thanks again.

    https://support.advancedcustomfields.com/forums/topic/wp_query-with-meta_query-on-a-subfield-within-a-group-field/

Viewing 8 posts - 26 through 33 (of 33 total)

The topic ‘Meta query for post object?’ is closed to new replies.