0

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.

  • 1
    In general, the_ prefixed functions echos its output and get_ prefixed functions returns – Pieter Goosen Mar 17 '16 at 4:28
3

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.

  • 2
    In general, the_ prefixed functions echos its output and get_ prefixed functions returns – Pieter Goosen Mar 17 '16 at 4:27
  • so how would i call this function on a page to check if that slug exists and then echo the page title for that page i just looked up? – John Carlos Mar 17 '16 at 4:50
  • Thanks Pieter. I have made necessary changes. @JohnCarlos - I have added function call at the end of my answer. – Prasad Nevase Mar 17 '16 at 8:47
2

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;
    }
}
0

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;
  • In general, the_ prefixed functions echos its output and get_ prefixed functions returns – Pieter Goosen Mar 17 '16 at 4:27
  • 1
    this appears to just output the post slug 'post_name' when i need to output the post_title and the if is_page will only work if i'm actually on that page, not as a lookup to see if it exists – John Carlos Mar 17 '16 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.