最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - WP_Query, ACF field and array

programmeradmin0浏览0评论

First of all, sorry this title, I really don't know how title the question without write a blobtext! :)

Anyway, my problem is this one: I have a custom post "Risorse" (en: Resources). It supports only titles, all other parameters are custom fields created with ACF. One of these is a relationship where I can select one or more pages. So, for example I can create a Resource and select a relationship with two pages. In DB it is saved in this way

a:2:{i:0;s:3:"727";i:1;s:3:"729";}

Now, when I load for example the page 727, I want to list all Resources that have a relation with that page.

I'm trying a custom query but I don't know how set the meta_query array. I tried something like:

'meta_query' => array(
    array(
        'key' => 'product',
        'value' => get_the_id(),
        'compare' => 'IN'
    )
)

Obviously no posts are returned because the meta_key 'product' contains an array of values. How can I get all Resources the have a relation with a specific post?

First of all, sorry this title, I really don't know how title the question without write a blobtext! :)

Anyway, my problem is this one: I have a custom post "Risorse" (en: Resources). It supports only titles, all other parameters are custom fields created with ACF. One of these is a relationship where I can select one or more pages. So, for example I can create a Resource and select a relationship with two pages. In DB it is saved in this way

a:2:{i:0;s:3:"727";i:1;s:3:"729";}

Now, when I load for example the page 727, I want to list all Resources that have a relation with that page.

I'm trying a custom query but I don't know how set the meta_query array. I tried something like:

'meta_query' => array(
    array(
        'key' => 'product',
        'value' => get_the_id(),
        'compare' => 'IN'
    )
)

Obviously no posts are returned because the meta_key 'product' contains an array of values. How can I get all Resources the have a relation with a specific post?

Share Improve this question edited Mar 28, 2019 at 22:32 NightHawk 1,48415 silver badges21 bronze badges asked Mar 28, 2019 at 13:28 globdugglobdug 32 silver badges5 bronze badges 2
  • OK, I solved in this way: 'meta_query' => array( array( 'key' => 'product', 'value' => '"'.get_the_id().'"', 'compare' => 'LIKE' ) ) – globdug Commented Mar 28, 2019 at 14:18
  • Go ahead and add your comment as the answer, and then accept it. – NightHawk Commented Mar 29, 2019 at 15:12
Add a comment  | 

1 Answer 1

Reset to default 0

OK, I solved in this way:

'meta_query' => array(
  array(
    'key' => 'product',
    'value' => '"'.get_the_id().'"',
    'compare' => 'LIKE'
  )
)
发布评论

评论列表(0)

  1. 暂无评论