0

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++;
}
5
  • 1
    What do you mean by "doesn't work"? Can you elaborate? Do you get an error? Is the _wp_page_template meta value always empty or never saved? ...
    – birgire
    Jun 3, 2014 at 3:07
  • hmm... great question. when clicking the fast edit on a page item i see the select as if nothing is selected - here is an image to make it more clear - tinypic.com/r/nvwfas/8
    – Sagive
    Jun 3, 2014 at 13:03
  • ps.. no error is returnd... simply put it - noting happens.
    – Sagive
    Jun 4, 2014 at 0:31
  • hmm, and the page templates are correct (exists)?
    – birgire
    Jun 4, 2014 at 8:38
  • Yep, they exist. Also, they have a 'template name' decleration as you answered... thats a weird one. i am trying to reproduce in a different installation.
    – Sagive
    Jun 4, 2014 at 12:33

1 Answer 1

1

I think I found the reason for your problem.

You must remember to add the corresponding template header:

<?php
/**
 * Template Name: Page Tools
 */

to your custom template files page-tools.php, ...

I hope this helps.

9
  • thanks mate but i did remember to do that. ;( thats not the issuse...
    – Sagive
    Jun 4, 2014 at 12:32
  • I tested your code with update_post_meta and it worked for me.
    – birgire
    Jun 4, 2014 at 13:14
  • Really?! ok.. going to test. (thanks for taking the time) - be right back
    – Sagive
    Jun 4, 2014 at 13:20
  • 1
    Ok... it does work on one page which suggests i need to debug it and its not a wordpress issuse.. thanks a lot for your time and help ;)
    – Sagive
    Jun 4, 2014 at 19:13
  • 1
    thanks a lot for all your help mate. going to try while monitoring the DB. have a gr8 day ;)
    – Sagive
    Jun 4, 2014 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.