duanqi6274 2018-02-02 10:19
浏览 34
已采纳

为什么这个get_pages返回一个空数组?

I'm trying to add a meta box for my custom post type (which is inside a plugin).

I want to get all pages that are assigned a particular page template in my Wordpress site, so I use the following:

$surgeons = get_pages(array(
    'meta_key' => '_wp_page_template',
    'meta_value' => 'template-meet-the-team-single.php'
));

However, $surgeons is returned as an empty array, why?

It sounds like no page is using the template I'm stating, as if I just use the default get_pages() it returns all pages, but this is incorrect as I definitely have pages that are assigned this template.

Does it matter that this code is in a plugin file, and the meta_value is trying to locate this template in the plugin folder?

  • 写回答

2条回答 默认 最新

  • dongxiee0744 2018-02-04 09:00
    关注

    Although I don't think this really provides an answer to the issue I Was having, this was the way to fix the array being returned and provide the correct results.

    Instead of using get_pages(), I simply switched it to get_posts(), which then returned the correct results.

    $surgeons = get_posts(array(
        'post_type' => 'page',
        'posts_per_page' => -1,
        'meta_key' => '_wp_page_template',
        'meta_value' => 'template-meet-the-team-single.php'
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助