Home > Blockchain >  Query posts with custom field is array
Query posts with custom field is array

Time:03-15

I'm using ACF to create custom field of select type. I used like this to create custom select field like this:

1 : Anh
2 : Hai
3 : Hoang
4 : May
....
12 : Lan

I use this query to search posts have value is 1 (Anh) like this:

                 URL for query :   /?thename=1
                 $thename = $_GET['thename'];
                 $args = array(
                            'post_type' => 'interviews',
                            'post_status'    => 'publish',
                            'posts_per_page' => $perpage,
                            'paged'          => get_query_var('paged'),
                            'meta_query'    => array(
                                    'key'       => 'thename',
                                    'value'     => $thename,
                                    'compare'   => 'LIKE',
                                )

I searched like this but it returns wrong results. I think the value of thename field is array (value and label) then It is not possible to use query above.

So can you suggest me to do with this search? Thanks.

CodePudding user response:

Your meta query should be an array within an array... update the meta_query like below:

'meta_query' => array(
    array(
        'key'       => 'thename',
        'value'     => $thename,
        'compare'   => 'LIKE',
    )
)

CodePudding user response:

According to the meta_query documentation meta_query must contains one or more arrays, so your query must be something look like this :

'meta_query' => [
  [
    'key'       => 'thename',
    'value'     => $thename,
    'compare'   => 'LIKE',
  ]
]
  • Related