Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a WPMU instance that works less like a network of blogs and more like a holistic application. I'm needing to do a check and see if 3 pages with the slugs 'home', 'login', and 'password' exist. If not, I need the system to generate them automatically. If it does, I need the system to ignore.

Right now I have the following code, and for some reason it is generating 5 posts each time a page is loaded. Anyone have advice on how I could better accomplish this?

function check_pages_live(){
        if(get_page_by_title( 'home', page ) != NULL) {
            create_pages_fly('home');
        }
        if(get_page_by_title( 'login', page ) != NULL) {
            create_pages_fly('login');
        }
        if(get_page_by_title( 'password', page ) != NULL) {
            create_pages_fly('password');
        }
    }
    add_action('init','check_pages_live');
    function create_pages_fly($pageName) {
        $createPage = array(
          'post_title'    => $pageName,
          'post_content'  => 'Starter content',
          'post_status'   => 'publish',
          'post_author'   => 1,
          'post_type'     => 'page',
          'post_name'     => $pageName
        );

        // Insert the post into the database
        wp_insert_post( $createPage );
    }
share|improve this question
1  
I would try get_page_by_path, since get_page_by_title can return an array, that's the only logical reason it is returning 5. –  Wyck Jan 30 '13 at 23:32
add comment

2 Answers 2

up vote 1 down vote accepted

I think you want:

if( get_page_by_title( 'home' ) == NULL )
    create_pages_fly( 'home' );

Your original if condition said if the page exists (does not equal NULL), then create the page. Also, the 2nd argument should be a string, though it doesn't really matter in this case since it'll just default to 'page' anyway.

share|improve this answer
    
Doh! Can't believe I did that. Must have been a curse of staring at code for too long today. This is perfect. Thanks for the assistance! –  Jake Lisby Jan 30 '13 at 23:50
add comment

Very handy function, I'll be adapting this for some of my work. One minor enhancement I'd make to streamline it a bit more is to put the get_by_title() action into a function, so you can just call that on each page.

Ex:

function create_page_if_null($target) {
    if( get_page_by_title($target) == NULL ) {
        create_pages_fly($target);
    }
}

function check_pages_live(){
    create_page_if_null('about');
    create_page_if_null('contact-us');
    create_page_if_null('home');
    create_page_if_null('news');
    create_page_if_null('privacy');
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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