Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is is possible to get a page's permalink from the slug alone? I'm aware that you can get the page's permalink from the ID using get_page_link():

<a href="<?php echo get_page_link(40); ?>">Map</a>

I'm curious if there is any way to do the same with the slug of a page:

<a href="<?php echo get_page_link('map'); ?>">Map</a>
share|improve this question
add comment

3 Answers 3

up vote 40 down vote accepted

You're talking about Pages right? Not Posts.

Is this what you looking for:

  1. get_permalink( get_page_by_path( 'map' ) )
  2. get_permalink( get_page_by_title( 'Map' ) )
  3. home_url( '/map/' )
share|improve this answer
    
Did you mean get_permalink(get_page_by_path('contact')->ID));? –  Jonathan Sampson Dec 7 '10 at 22:22
    
hmmm nope? What's with the ID? –  zeo Dec 8 '10 at 1:52
2  
get_page_by_path() returns an array of all page information. get_permalink() takes a page ID as the first argument. I thought I'd have to explicitly pass the ID value. –  Jonathan Sampson Dec 8 '10 at 4:47
7  
@Jonathan: It's not always documented, but many WP functions accept both numeric ID's and full post objects as the argument. –  Jan Fabry Dec 8 '10 at 7:18
    
It seems that get_page_by_path() can be quite complicated to use when dealing with child pages... –  Kaaviar Nov 14 '11 at 14:57
show 1 more comment

I think this could be better:

function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) {
    global $wpdb;
    $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $page_slug, $post_type ) );
    if ( $page )
            return get_page($page, $output);
    return null;
}

following the pattern of "original" get_page_by_title of wordpress. (line 3173)

rgds

share|improve this answer
4  
Why would that be better? Can you explain? –  julien_c Apr 11 '12 at 13:11
    
Last comment - I think that sql needs to have one more condition: function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) { global $wpdb; $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) ); if ( $page ) return get_page($page, $output); return null; } –  user25790 Jan 9 '13 at 14:27
    
Why? It doesn't generate a complete post object just to get the ID. –  s_ha_dum Jan 9 '13 at 14:53
    
@webcitron I think just because is following original pattern of Wordpress getting post by 'title', just changing for 'slug'. (check the link) –  Matheus Eduardo May 13 '13 at 11:08
add comment

This is a method published by Tom McFarlin on his blog:

/**
 * Returns the permalink for a page based on the incoming slug.
 *
 * @param   string  $slug   The slug of the page to which we're going to link.
 * @return  string          The permalink of the page
 * @since   1.0
 */
function wpse_4999_get_permalink_by_slug( $slug, $post_type = '' ) {

    // Initialize the permalink value
    $permalink = null;

    // Build the arguments for WP_Query
    $args = array(
        'name'          => $slug,
        'max_num_posts' => 1
    );

    // If the optional argument is set, add it to the arguments array
    if( '' != $post_type ) {
        $args = array_merge( $args, array( 'post_type' => $post_type ) );
    }

    // Run the query (and reset it)
    $query = new WP_Query( $args );
    if( $query->have_posts() ) {
        $query->the_post();
        $permalink = get_permalink( get_the_ID() );
    }
    wp_reset_postdata();

    return $permalink;
}

It works with custom post types and built-in post types (such as post and page).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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