Menu Close

WordPress Trick: Get Page Permalink From Page Title


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'); ?>
View Source
Posted in WordPress