Support » Fixing WordPress » How to check if Page exists?

  • I am using a page as the front page with query_posts as follows:


    It works great, but I want the blog to revert back to the default 10 posts display automatically, in case there is no Page called home (i.e., the page doesn’t exist).

    Is there any way — using conditional tags, or something else — to check if the Page called “Home” exists first and then, if TRUE = serve the query_posts command, if FALSE = serve the default setting?

    Hope I am clear about what I want. Thanks in advance! 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • You can use condition tags:

    The one that will interest you the most is the following one:

    is_page(‘About Me’)

    Thanks, TwoMotifs, but that’s not what I need (at least, I think it’s not).

    I don’t want to serve template-specific content based on the template file being called (which is what is_page() would be useful for)…

    Instead, I want to insert contents of a particular Page in my template file using query_posts(), provided that the Page exists in the database. If the Page doesn’t exist, I want to serve the template without the ‘query_posts()’. Along the lines of…

    1. Does the page ‘pagename’ exist?
    2. If yes, run template with query_posts().
    3. If not, run template without query_posts().


    Bump 🙂

    Last bump… 🙁

    Nobody knows the answer to this question???

    Just came here looking for the answer to this same question. Had to figure it out myself. Hope this helps:

    <?php $name = $wpdb->get_var(“SELECT ID FROM $wpdb->posts WHERE post_name=’your-page-name'”); ?>
    <?php if ($name != ”) { ?>
    Do this
    <?php } else { ?>
    It doesn’t exist
    <?php } ?>

    Create this function somewhere (like functions.php of your theme)

    function get_page_by_name($pagename)
    $pages = get_pages();
    foreach ($pages as $page) if ($page->post_name == $pagename) return $page;
    return false;

    $page = get_page_by_name('MYPAGE');
    if (!empty($page)) {
    // page exists and is in $page
    } else {
    // page does not exist

    Sounds promising. Will give it a try. Thanks! 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to check if Page exists?’ is closed to new replies.