Support

Account

Home Forums General Issues Create meta_query with relationship field?

Solved

Create meta_query with relationship field?

  • I want to create a custom WP_Query where I’m querying all posts with a certain relationship value, where the output is set as a Post Object.

    The output of the field is:

     bedrijf => 
         0 => string(19) "a:1:{i:0;s:2:"16";}" 

    The 16 in the example above, is the ID of the corresponding post.

    When I try the query below, it just outputs all existing posts, not just the ones responding to the ID 16.

    $args = array (
    							'meta_query' => array(
    								array(
    									'key' => 'bedrijf', // name of custom field
    									'value' => 16, // matches exaclty "123", not just 123. This prevents a match for "1234"
    									'compare' => 'LIKE'
    								)
    							)
    );

    What is going wrong here?

    Thanks!

  • Actually, I found out what went wrong. I had another meta_query which overruled the one sent above.

    Consider this question as solved.

  • Just came across this answer and thought it best to point out the the example above would cause issues with an id that has 16 in as it would match 116,161,1216, etc. and so the quotes around the value are important as per the comment.

    So…

    
    $args = array (
        'meta_query' => array(
            array(
                'key' => 'bedrijf', // name of custom field
                'value' => '"16"', // matches exaclty "16", not just 161. This prevents a match for "1234"
                'compare' => 'LIKE'
            )
        )
    );
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Create meta_query with relationship field?’ is closed to new replies.