从WordPress DB检查元值不适用于页面

时间:2019-06-11 15:07:26

标签: php database wordpress

我希望在用户选择某个页面模板时创建CPT(投资组合)。

这是我到目前为止使用的代码,可用于帖子:

$posts_with_meta = get_posts(array(
    'posts_per_page' => 1,
    'meta_key' => '_wp_page_template',
    'meta_value' => 'portfolio.php',
    'fields' => 'ids',
));

if (count($posts_with_meta)) {
    function spx_pt_portfolio()
    {
        register_post_type('portfolio',
            array(
                'labels' => array(
                    'name' => __('Portfolio'),
                    'singular_name' => __('Portfolio'),
                ),
                'public' => true,
                'has_archive' => true,
                'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'revisions'),
            )
        );
    }

    add_action('init', 'spx_pt_portfolio');
}

由于某些原因,此代码仅适用于帖子。当我在页面上选择页面模板“ Portfolio”时,什么也没发生。

1 个答案:

答案 0 :(得分:0)

不确定我确切了解您要执行的操作,但这可能是您正在使用get_posts()并且需要在页面上使用get_pages()的原因。