WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've made so that everytime my plugin is activated it's supposed to create a page. Although i want it to only create the page if it doesn't exist from before. But what it's doing is that it's creating another page with kns-products-1 instead of not doing it at all.

My code so far:

function kns_install() {
    global $wp_version;

    if( version_compare( $wp_version, '3.5', '<' ) ) {
        wp_die( 'Detta tilläget kräver att du har WordPress version 3.5 eller högre.' );
    } else {
        if(!is_page('kns-products')) {
            $product_page = array(
            'post_type' => 'page',
            'post_name' => 'kns-products',
            'post_title' => 'Produkter',
            'post_status' => 'publish',


I thought that the !is_page condition would solve this but there seems to be a built in code to just add numbers after the slug name.

Is there any way to solve this or does anyone know of any better approach?

share|improve this question
Please see the Codex. is_page does not work like does the page I provide as argument exist?. – tfrommen Sep 16 '13 at 17:41
@tf Oh, I'm sorry, I should have read more.. I googled if there was any "page_exists" and is_page was all that came up. Does anything like that exist? – Jake Snake Sep 16 '13 at 17:49
up vote 0 down vote accepted

Since you want to check for a certain page, you could use one of the following functions, for instance:

  • get_post: Takes a post ID and returns the database record for that post, which can also be a page.
  • get_page_by_title: Retrieves a post given its title. If more that one post uses the same title, the post with the smallest ID will be returned.

Or you could write your own little function to get the page by its slug:

function get_page_by_slug($slug) {
    if ($pages = get_pages())
        foreach ($pages as $page)
            if ($slug === $page->post_name) return $page;
    return false;
} // function get_page_by_slug

You would use it like so then:

if (! get_page_by_slug('kns-products')) {
share|improve this answer
Thanks, but I managed to solve it by creating a variable with get_pages! Although I would probably use your method if I were to do this more than once in my plugin! – Jake Snake Sep 16 '13 at 18:08

Your Answer


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.