Support » Plugins » Check if page exists else create it

  • I’m trying to run a function that checks to see if a page with a specific title has already been created. If not then it should create it.

    I’ve tried a variety of methods and they all seem to end up with the same issue. They either create a page but when I deactivate the plugin and reactivate they create the same titled page again. Or it simply does nothing:

    Here is my code (function):

    function create_page() {
    		global $wpdb;
    
    		$name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name='some_page'");
    	 if ($name == '') { 
    
    		global $user_ID;
    
    		$page['post_type']    = 'page';
    		$page['post_content'] = '';
    		$page['post_parent']  = 0;
    		$page['post_author']  = $user_ID;
    		$page['post_status']  = 'publish';
    		$page['post_title']   = 'some_page';
    		$page = apply_filters('yourplugin_add_new_page', $page, 'teams');
    		$pageid = wp_insert_post ($page);
    
    		}
    }
    
    register_activation_hook(__FILE__, 'create_page');

    This code creates another page as long as there is a page with the same title. But it cannot create the page if there isn’t a page with the same title!

    I’ve also tried this bit of code with the same problem. I must be missing something!

    $pages = get_pages();
    
    foreach ($pages as $page) {
    	$apage = $page->post_name;
    
            if ($apage == 'discussions') {
    		// doe something here
    	}
    }

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Snaphaan

    (@snaphaan)

    bump

    Get the pages.
    Iterate throught the pages.
    If the page with the title ‘Boodskap is found…
    don’t do nuttin.
    else…
    create the page.

    The idea seems simple enough as well as the code but it does not seem to work that way.

    Has anyone done anything similar?

    I had the same issue. I had created a simple plugin to include a page called ‘Movies’ and when i activate the plugin the page is created but when i deactivate and re-activate it, a duplicate ‘Movies’ page is created. I solved the issue by putting a simple condition before my main code:

    if (get_page_by_title('Movies') == NULL)

    The full code:

    function create_movies_pages() {
        //post status and options
        if (get_page_by_title('Movies') == NULL) {
            $post = array(
                'comment_status' => 'open',
                'ping_status' => 'closed',
                'post_date' => date('Y-m-d H:i:s'),
                'post_name' => 'movies',
                'post_status' => 'publish',
                'post_title' => 'Movies',
                'post_type' => 'page',
            );
            //insert page and save the id
            $newvalue = wp_insert_post($post, false);
            //save the id in the database
            update_option('movpage', $newvalue);
        }
    }
    
    // Activates function if plugin is activated
    register_activation_hook(__FILE__, 'create_movies_pages');

    Note: Sometimes get_page_by_title may return true if the page exists in trash, so clear the trash and then try.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Check if page exists else create it’ is closed to new replies.