【问题标题】:Wordpress & Advanced Custom Fields - Retrieving categoriesWordpress 和高级自定义字段 - 检索类别
【发布时间】:2015-07-31 11:40:28
【问题描述】:

在使用高级自定义字段将帖子/附件连接到 Woocommerce 类别后,我似乎在检索帖子/附件时遇到了问题。

我已经尝试使用详细的高级示例here,这与我使用该示例所能达到的一样接近:

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

它仍然不工作,我不太清楚为什么。

我查看了 db 条目,有问题的条目是:

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”以使用正确的类型。目前还没有工作。

【问题讨论】:

    标签: php wordpress woocommerce advanced-custom-fields


    【解决方案1】:

    您的帖子类型标记为attachments,但应为attachment。帖子类型是单数的。

    【讨论】:

    • 谢谢你。我已将其更改为attachment,但目前仍然无法使用。
    【解决方案2】:

    找到问题的解决方案。显然我使用了错误类型的 compare 值,并且该类别的实际值必须有双引号。

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

    这就是我的最终解决方案的样子:

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

    【讨论】:

      猜你喜欢
      • 2017-08-28
      • 1970-01-01
      • 2012-08-10
      • 2015-01-21
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 2015-05-25
      相关资源
      最近更新 更多