Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Prevent Duplicate Pages in Automatic Page Generation WordPress

  • SOLVED

I use the code below to generate a page called 'Disclaimer' if there is currently not a disclaimer page on the blog. This code works well. The problem is that if a user deletes the 'Disclaimer' page, this code will generate the page multiple times over again.

I would like this code to be modified to prevent multiple pages from being generated if the page is deleted.


// If there is no disclaimer, generate one from its template
$disclaimer_query = new WP_Query(array(
'pagename' => 'disclaimer',
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 1,
));
if (! $disclaimer_query->post_count)
wp_insert_post(array(
'post_name' => 'disclaimer',
'post_title' => 'Disclaimer',
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'page_template' => 'page.php',
));
unset($disclaimer_query);

Answers (1)

2014-01-05

Hariprasad Vijayan answers:

Hello,

Try this code,

$page = get_page_by_title( 'Disclaimer' );
if(!$page)
{
wp_insert_post(array(
'post_name' => 'disclaimer',
'post_title' => 'Disclaimer',
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'page_template' => 'page.php',
));
}