i have been trying to attach multiple pages, eash to its page-template using a foreach loop... Manually meaning writing each page code works but inside the foreach loop it wont work.
I have several of these foreach loops so i would rather not do it manually and would also like to know / understand the issuse... here is my code
- notice that just adding a custom field with a value works.. (weird)
- what am i doing wrong?
.
$start_number = 2;
$iim_pages = array('tools', 'clients', 'users', 'resources', 'data', 'data2', 'settings');
foreach($iim_pages as $page) {
$page_title = ucwords(str_replace('-', ' ', $page));
$page_filename = 'page-'.$page.'.php';
//don't change the code bellow, unless you know what you're doing
$page_check = get_page_by_title($page_title);
$new_page = array(
'post_type' => 'page',
'menu_order' => $start_number,
'post_title' => $page_title,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
);
if(!isset($page_check->ID)){
$new_page_id = wp_insert_post($new_page);
// DOESENT WORK WITH ADD_POST_META or UPDATE_POST_META
add_post_meta($new_page_id, '_wp_page_template', $page_filename);
// THIS WORKS!
add_post_meta($new_page_id, 'testmeta', $page_filename);
}
$start_number++;
}
_wp_page_template
meta value always empty or never saved? ...