Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Okay so I'm using the relationship field from Advanced Custom Fields against a custom post type. The relationship lists countries, when these are saved they are within a serialised array:

[country] => Array
    (
        [0] => a:2:{i:0;s:2:"19";i:1;s:2:"72";}
    )

[_country] => Array
    (
        [0] => field_5049ccf3aac29
    )

I want to query the custom post type by a single country from within this serialised array, but it's not in a very good format for me to do so.

In SQL I'd be tempted to do a LIKE but that's neither ideal or available via WP_Query

Any ideas on how I can achieve this? I guess I could do a bit of custom SQL to return all post IDs for this CPT where the country has the country ID I'm after, I'm just hoping I'm missing a very obvious / cleaner solution.

Edit 1

So I've got it working using the following code, but it's not ideal:

    <?php

        $query = new WP_Query(
            array(
                'post_type'     => 'some_post_type',
                'meta_query'    => array(
                    array(
                        'key'       => 'country',
                        'compare'   => 'LIKE',
                        'value'     => '"' . get_the_ID() . '"'
                    )
                )
            )
        );

    ?>

This approach I mentioned before backed up by this similar question, again, not ideal but if anyone knows of a better solution I'm all ears.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Answering my own question, as suggested in my edit I've had to use the following meta_query array to query the posts:

'meta_query'    => array(
    array(
        'key'       => 'country',
        'compare'   => 'LIKE',
        'value'     => '"' . get_the_ID() . '"'
    )
)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.