I want that a CPT (Portfolio) gets created when a user is selecting a certain page template.
Here is the code I have so far, that works with posts:
$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');
}
For some reason, this code only works with posts. When I select the page template "Portfolio" on a page, nothing happens.