admin管理员组

文章数量:1130349

Came across this function to check if a page exists by its slug and it works great

function the_slug_exists($post_name) {
global $wpdb;
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
    return true;
} else {
    return false;
}

}

but what i'm needing to do now is check if that exists, then output the title for that page, any help is appreciated.

so when i put this code on the page to lookup the slug:

<?php if (the_slug_exists('page-name')) {
                    echo '$post_title';
} ?>

everything is perfect other than the fact I need the function to output the title as well as lookup the slug, hopefully that makes more sense.

Came across this function to check if a page exists by its slug and it works great

function the_slug_exists($post_name) {
global $wpdb;
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
    return true;
} else {
    return false;
}

}

but what i'm needing to do now is check if that exists, then output the title for that page, any help is appreciated.

so when i put this code on the page to lookup the slug:

<?php if (the_slug_exists('page-name')) {
                    echo '$post_title';
} ?>

everything is perfect other than the fact I need the function to output the title as well as lookup the slug, hopefully that makes more sense.

Share Improve this question edited Mar 17, 2016 at 4:42 John Carlos asked Mar 17, 2016 at 1:23 John CarlosJohn Carlos 31 gold badge1 silver badge3 bronze badges 1
  • 1 In general, the_ prefixed functions echos its output and get_ prefixed functions returns – Pieter Goosen Commented Mar 17, 2016 at 4:28
Add a comment  | 

3 Answers 3

Reset to default 6

looking at MySQL query in the function it becomes clear that though it matches the post_name and returns true or false. What you want to find is, if a page exists by its slug. So, in that case, the function you have chosen is wrong.

To get the page by title you can simply use:

$page = get_page_by_title( 'Sample Page' );
echo $page->title

Just in case if you want to get page by slug you can use following code:

function get_page_title_for_slug($page_slug) {

     $page = get_page_by_path( $page_slug , OBJECT );

     if ( isset($page) )
        return $page->post_title;
     else
        return "Match not found";
}

You may call above function like below:

echo "<h1>" . get_page_title_for_slug("sample-page") . "</h1>";

Let me know if that helps.

It is almost always never adviced to run custom SQL when WordPress have native functions to do a specific job. The slug or post_name of a page is also the used in the page's path (URL) to identify and return the content of that page when such a page is visited. This means that, we can use get_page_by_path() and just pass the slug (post_name) to the function and get the returned page object.

get_page_by_path ( 
    string $page_path, 
    string $output = OBJECT, 
    string|array $post_type = 'page' 
)

Like many other functions and naming conventions (like post_name which is actually the slug, and not the name) in WordPress, get_page_by_path()'s name is confusing and IMHO stupid as you can pass any post type to the function

We can use get_page_by_path() which will return nothing if the page is not found, or the page object on success. Lets write a proper wrapper function which will return false on failure and will always return the page object regardless if the page exists

function get_post_by_post_name( $slug = '', $post_type = '' )
{
    //Make sure that we have values set for $slug and $post_type
    if (    !$slug
         || !$post_type
    )
        return false;

    // We will not sanitize the input as get_page_by_path() will handle that
    $post_object = get_page_by_path( $slug, OBJECT, $post_type );

    if ( !$post_object )
        return false;

    return $post_object;
}

You can then use it as follow

if ( function_exists( 'get_post_by_post_name' ) ) {
    $post_object = get_post_by_post_name( 'post-name-better-known-as-slug', 'your_desired_post_type' );
    // Output only if we have a valid post 
    if ( $post_object ) {
        echo apply_filters( 'the_title', $post_object->post_title );
    }
}

If you really just need the post title returned, we can slightly alter the function

function get_post_title_by_post_name( $slug = '', $post_type = '' )
{
    //Make sure that we have values set for $slug and $post_type
    if (    !$slug
         || !$post_type
    )
        return false;

    // We will not sanitize the input as get_page_by_path() will handle that
    $post_object = get_page_by_path( $slug, OBJECT, $post_type );

    if ( !$post_object )
        return false;

    return apply_filters( 'the_title', $post_object->post_title );
}

and then use it as follow

if ( function_exists( 'get_post_title_by_post_name' ) ) {
    $post_title = get_post_title_by_post_name( 'post-name-better-known-as-slug', 'your_desired_post_type' );
    // Output only if we have a valid post 
    if ( $post_title ) {
        echo $post_title;
    }
}

Here is an update to the same code for you

function the_slug_exists($post_name) {
global $wpdb;
//Sanitize title
$post_name = sanitize_title($post_name);
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
    return $post_name;
} else {
    return false;
}

then you can run it by simply calling it with the echo function as in the following code, which will only print the post name if it exists.

echo the_slug_exists($post_name); 

UPDATE

Alternatively you can rely on the default conditional WordPress function is_page() so your code becomes as simple as follows..

if( is_page($post_name) ):

   echo $post_name; 

endif;

Came across this function to check if a page exists by its slug and it works great

function the_slug_exists($post_name) {
global $wpdb;
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
    return true;
} else {
    return false;
}

}

but what i'm needing to do now is check if that exists, then output the title for that page, any help is appreciated.

so when i put this code on the page to lookup the slug:

<?php if (the_slug_exists('page-name')) {
                    echo '$post_title';
} ?>

everything is perfect other than the fact I need the function to output the title as well as lookup the slug, hopefully that makes more sense.

Came across this function to check if a page exists by its slug and it works great

function the_slug_exists($post_name) {
global $wpdb;
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
    return true;
} else {
    return false;
}

}

but what i'm needing to do now is check if that exists, then output the title for that page, any help is appreciated.

so when i put this code on the page to lookup the slug:

<?php if (the_slug_exists('page-name')) {
                    echo '$post_title';
} ?>

everything is perfect other than the fact I need the function to output the title as well as lookup the slug, hopefully that makes more sense.

Share Improve this question edited Mar 17, 2016 at 4:42 John Carlos asked Mar 17, 2016 at 1:23 John CarlosJohn Carlos 31 gold badge1 silver badge3 bronze badges 1
  • 1 In general, the_ prefixed functions echos its output and get_ prefixed functions returns – Pieter Goosen Commented Mar 17, 2016 at 4:28
Add a comment  | 

3 Answers 3

Reset to default 6

looking at MySQL query in the function it becomes clear that though it matches the post_name and returns true or false. What you want to find is, if a page exists by its slug. So, in that case, the function you have chosen is wrong.

To get the page by title you can simply use:

$page = get_page_by_title( 'Sample Page' );
echo $page->title

Just in case if you want to get page by slug you can use following code:

function get_page_title_for_slug($page_slug) {

     $page = get_page_by_path( $page_slug , OBJECT );

     if ( isset($page) )
        return $page->post_title;
     else
        return "Match not found";
}

You may call above function like below:

echo "<h1>" . get_page_title_for_slug("sample-page") . "</h1>";

Let me know if that helps.

It is almost always never adviced to run custom SQL when WordPress have native functions to do a specific job. The slug or post_name of a page is also the used in the page's path (URL) to identify and return the content of that page when such a page is visited. This means that, we can use get_page_by_path() and just pass the slug (post_name) to the function and get the returned page object.

get_page_by_path ( 
    string $page_path, 
    string $output = OBJECT, 
    string|array $post_type = 'page' 
)

Like many other functions and naming conventions (like post_name which is actually the slug, and not the name) in WordPress, get_page_by_path()'s name is confusing and IMHO stupid as you can pass any post type to the function

We can use get_page_by_path() which will return nothing if the page is not found, or the page object on success. Lets write a proper wrapper function which will return false on failure and will always return the page object regardless if the page exists

function get_post_by_post_name( $slug = '', $post_type = '' )
{
    //Make sure that we have values set for $slug and $post_type
    if (    !$slug
         || !$post_type
    )
        return false;

    // We will not sanitize the input as get_page_by_path() will handle that
    $post_object = get_page_by_path( $slug, OBJECT, $post_type );

    if ( !$post_object )
        return false;

    return $post_object;
}

You can then use it as follow

if ( function_exists( 'get_post_by_post_name' ) ) {
    $post_object = get_post_by_post_name( 'post-name-better-known-as-slug', 'your_desired_post_type' );
    // Output only if we have a valid post 
    if ( $post_object ) {
        echo apply_filters( 'the_title', $post_object->post_title );
    }
}

If you really just need the post title returned, we can slightly alter the function

function get_post_title_by_post_name( $slug = '', $post_type = '' )
{
    //Make sure that we have values set for $slug and $post_type
    if (    !$slug
         || !$post_type
    )
        return false;

    // We will not sanitize the input as get_page_by_path() will handle that
    $post_object = get_page_by_path( $slug, OBJECT, $post_type );

    if ( !$post_object )
        return false;

    return apply_filters( 'the_title', $post_object->post_title );
}

and then use it as follow

if ( function_exists( 'get_post_title_by_post_name' ) ) {
    $post_title = get_post_title_by_post_name( 'post-name-better-known-as-slug', 'your_desired_post_type' );
    // Output only if we have a valid post 
    if ( $post_title ) {
        echo $post_title;
    }
}

Here is an update to the same code for you

function the_slug_exists($post_name) {
global $wpdb;
//Sanitize title
$post_name = sanitize_title($post_name);
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
    return $post_name;
} else {
    return false;
}

then you can run it by simply calling it with the echo function as in the following code, which will only print the post name if it exists.

echo the_slug_exists($post_name); 

UPDATE

Alternatively you can rely on the default conditional WordPress function is_page() so your code becomes as simple as follows..

if( is_page($post_name) ):

   echo $post_name; 

endif;

本文标签: functionsCheck if Page Slug Existsthen display that Page39s Title