如何获取具有特定附件图像集的自定义帖子类型帖子的数量?

时间:2021-03-08 作者:Charles Davel

我目前正在使用它来筛选只有具有特定自定义图像附件字段值的帖子。但它返回0。我将此方法用于其他自定义字段,效果非常好。我是否需要做一些不同的事情来获取附件图像?

$args = array(
\'post_type\' => \'speakers\',
\'post_status\' => array(\'publish\', \'pending\', \'draft\', \'auto-draft\', \'future\', \'private\', \'inherit\'), 
\'post_per_page\' => -1,
\'meta_key\' => \'speaker_promo_image\',
\'meta_query\' => array(
    array(
        \'key\'     => \'speaker_promo_image\',
        \'value\' => \'\',
        \'compare\' => \'!=\',
    )
  ) 
);
// The Query
$posts = new WP_Query( $args );
$the_count = $posts->post_count;

1 个回复
SO网友:Charles Davel

我最终找到了答案。我安装了wordpress插件;“查询牧马人”;这帮我建立了我想要弄明白的东西。缺少的键不是自定义字段未出现的帖子上的“”值,而是我应该放“0”。

以下是最终工作代码:

$args = array (
 \'paged\' => 1,
 \'posts_per_page\' => \'-1\',
 \'offset\' => 0,
 \'post_status\' => array(\'publish\', \'pending\', \'draft\', \'auto-draft\', \'future\', \'private\', \'inherit\'),
 \'ignore_sticky_posts\' => 0,
 \'orderby\' => \'date\',
 \'order\' => \'DESC\',
 \'post_type\' => \'speakers\',
 \'meta_query\' => array (
    0 => array (
      \'key\' => \'speaker_promo_image\',
      \'value\' => \'0\',
      \'compare\' => \'!=\',
      \'type\' => \'CHAR\',
    ),
  ),
  \'meta_key\' => \'speaker_promo_image\',
);