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've created a class to dynamically create pages. I'm checking to see if the page exists by comparing the new page's title to post_name. The comparison seems to work ok, but even if there is a match the page is created anyway and I can't figure out why.

My code looks like this:

class createDynamicPage {

public function __construct( $nameArray ) {
    $this->title = $nameArray['title']; //Title of the page
    $this->slug = $nameArray['slug']; //Page slug
}

public $pageContent = '';

public function rps_createPage(){
    $allPages = get_pages();
    $exists = false;
    foreach( $allPages as $page ){
        if( strtolower( $page->post_name ) == strtolower( $this->title ) ){
            $exists = true;
        }
    }
    if( $exisits == false ) {
        $new_page_id = wp_insert_post(
            array(
                'post_title' => $this->title,
                'post_type'     => 'page',
                'post_name'  => $this->slug,
                'comment_status' => 'closed',
                'ping_status' => 'closed',
                'post_content' => $this->pageContent,
                'post_status' => 'publish',
                'post_author' => 1,
                'menu_order' => 0
            )
        );
    }
}

}

$cdArray = array( 'title' => 'Biography', 'slug' => 'concert-diary' );
$pageConcertDiary = new createDynamicPage( $cdArray );
add_action('init', array( &$pageConcertDiary, 'rps_createPage' ));

I assume I'm being very stupid, but I can't figure it out! Also, being a OOP newbie, I'd happy take criticism/pointers on how I've outlined the class.

Cheers

share|improve this question
    
why are you querying for all pages when you can just do one for a single page with that title/slug? –  onetrickpony Oct 25 '11 at 14:11
    
@OneTrickPony - how else do I check if the page already exists other than by checking all existing pages? –  Richard Sweeney Oct 25 '11 at 18:12
    
just request pages with the name, ie. $query = new WP_Query('name=concert-diary&posts_per_page=1&post_type=page');. If you don't get any results then the page doesn't exist. But there are dedicated functions for what you're trying to achieve, like get_page_by_title() –  onetrickpony Oct 25 '11 at 22:01

2 Answers 2

up vote 2 down vote accepted

Why are you comparing $post->post_name to $this->title? You should be comparing slug to slug.

Try changing your comparison from this:

if( strtolower( $page->post_name ) == strtolower( $this->title ) )

...to this:

if( strtolower( $page->post_name ) == strtolower( $this->slug ) )

Also, I might suggest keeping with the WordPress object conventions with your dynamically created pages, as doing so might help avoid such confusion.

I would change this:

$cdArray = array( 'title' => 'Biography', 'slug' => 'concert-diary' );

...to this:

$cdArray = array( 'title' => 'Biography', 'post_name' => 'concert-diary' );

And then change this:

public function __construct( $nameArray ) {
    $this->title = $nameArray['title']; //Title of the page
    $this->slug = $nameArray['slug']; //Page slug
}

...to this:

public function __construct( $nameArray ) {
    $this->title = $nameArray['title']; //Title of the page
    $this->post_name = $nameArray['post_name']; //Page slug
}

So that your comparison becomes this:

if( strtolower( $page->post_name ) == strtolower( $this->post_name ) )

That might help you avoid some confusion.

share|improve this answer
    
Hi @Chip - thanks for this. Makes sense of course and happily solved my problem. Cheers –  Richard Sweeney Oct 25 '11 at 18:36

At first, you may need to include wp-blog-header. then use this, to check if post already exist ( you can modify it to fit pages):

$title = 'mytitlee';

global $wpdb;
$id_ofpost_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$title'");
$id_ofpost_title = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title'");
if ($id_ofpost_name || $id_ofpost_title) {echo 'Exists, here is the id:'.$id_ofpost_title.' or '.$id_ofpost_name;} 
else {echo 'post wasnt found';}
share|improve this answer

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.