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)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Check if page exists else create it’ is closed to new replies.