Wordpress 和高级自定义字段 - 检索类别

Wordpress & Advanced Custom Fields - Retrieving categories

在使用高级自定义字段将它们连接到 Woocommerce 类别后,我似乎无法检索 posts/attachments。

我已经尝试使用详细的高级示例 here,这是我使用该示例所能获得的最接近的示例:

$images = get_posts(array(
  'numberposts' => -1,
  'post_type'   => 'attachment',
  'meta_query'  => array(
    'relation'    => 'AND',
    array(
      'key'       => 'category',
      'value'     => array('12', '13'),
      'compare'   => 'IN'
    )              
   )
));

它仍然无法正常工作,我不太清楚为什么。

我查看了数据库条目,有问题的条目是:

meta_id      post_id      meta_key                 meta_value
  242          70         category      a:2:{i:0;s:2:"12";i:1;s:2:"13";}

非常感谢任何帮助,并提前致谢。

编辑

更新:更改了 'post_type' 以使用正确的类型。目前还没有工作。

您的 post 类型标记为 attachments,但它应该是 attachment。 Post类型为单数。

找到问题的解决方法。显然我使用了错误类型的 compare 值,类别的实际值必须用双引号引起来。

我找到了我需要的示例代码here

以下是我的最终解决方案:

$images = get_posts(array(
  'numberposts' => -1,
  'post_type'   => 'attachment',
  'meta_query' => array(
    array(
      'key' => 'category',
      'value' => '"12"',
      'compare' => 'LIKE'
    )
  ),
));