Support

Account

Home Forums General Issues Query in relationship field

Solved

Query in relationship field

  • Hi guys,

    This is not an issue but a question.
    I want to query posts based on user relationship.
    You can sellect mutiple users and i don’t know how to query it.

    `$args = array(
    ‘numberposts’ => -1,
    ‘post_type’ => ‘posts’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘relation_user’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘=’,

    )
    )
    );

    i think the problem is that the query is an array and i want to check if it has te right value but in this case it’s just not equel to 1 because it’s an array.

  • This tutorial goes over everything you need to query relationship fields http://www.advancedcustomfields.com/resources/querying-relationship-fields/

    Basically, for what you want you need to use “LIKE” and the double quotes around the value are important.

    
    $args = array(
       'numberposts' => -1,
        'post_type' => 'post',
        'meta_query' => array(
            array(
                'key' => 'relation_user',
                'value' => '"1"',
                'compare' => 'LIKE',
           )
        )
    );
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Query in relationship field’ is closed to new replies.