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'
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dtudj42064 2018-02-02 10:33
    关注

    Please try by using custom query:

    $all_team_single =  query_posts(array(
            'post_type' =>'page',
            'meta_key'  =>'_wp_page_template',
            'meta_value'=>'template-meet-the-team-single.php',
        ));
    

    Your above code looking fine , to get more help please visit the below link. https://wordpress.stackexchange.com/questions/173469/loop-through-pages-with-specific-template

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 实验 4 FIFO 算法和 LRU 算法-C 程序实现
  • ¥30 电脑画面同步投屏,通过同wifi的方式投屏方法,接收投屏端不需要安装第三方软件,
  • ¥15 有偿拼接大疆精灵4RGB影像
  • ¥15 MATLAB特殊符号
  • ¥15 Arduino实现音频混响
  • ¥15 cuda.jit加速报错
  • ¥15 Octave 安装工具箱出错 Only Win32 target is supported!
  • ¥15 docker save的不能在另一台设备运行
  • ¥15 Unity Animation Rigging使用问题
  • ¥15 mbedtls握手返回-7200