Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

WordPress Trick: Get Page Permalink From Page Title

Last updated on:

As I was working on a premium theme which I will submit to ThemeForest next week, I wanted to allow users to select their portfolio page (which is based on a page template) from a drop-down in the admin panel so that it will show up in the breadcrumbs of the single-portfolio posts. Something which is great for usability and SEO but a bit tricky to do.

Basically I have to store all the WordPress pages in an array then allow the user to easily select one from a drop-down. The problem is that when a user selects one the output for that option becomes the page title, not the URL, Slug or ID. So doing a little Google search I came across a nice little function and modded it a bit so it allows you to get the permalink of any page based on the page title only.

Get Permalink From WordPress Page Title Function

Copy and paste the following function into your functions.php file:

// get permalink by title
function get_page_permalink_from_name($page_name) {
    global $post;
    global $wpdb;
    $pageid_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '" . $page_name . "' LIMIT 0, 1");
 return get_permalink($pageid_name);
}

Now use the following method to echo back the permalink for your desired page name:

<?php echo get_page_permalink_from_name('YOUR PAGE NAME'); ?>
Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

Comments

No comments yet. Why don't you kick off the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.