If you're an advanced developer, then you may want to skip directly to the code.

As soon as you begin working on an advanced plugin or an application in which you try to maintain some form of solid cohesion (or even an API), then you end finding that there’s a function you need that isn’t available in the Codex.

This isn’t unique to WordPress. Any programming language that has a rich API ultimately has a set of solid features that allow you to build upon them to write more power functions.

For me, I try to publish most of my helper functions here on the site. It makes things searchable, available, and even available for code review for the more critical readers :).

Case in point: In a recent project, I needed to find the permalink by slug in WordPress and there wasn’t a function available to do exactly that, so here’s what I ended up doing.

Getting The Permalink By Slug

I’ll discussing the following function in more detail after the code snippet, but the short of it is this:

  • Pass the function the slug of the post
  • Optionally provide post type
  • The function will return said permalink
/**
 * 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 example_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 ) );
	} // end if

	// 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();
	} // end if


	return $permalink;

} // end example_get_permalink_by_slug

The code comments should provide as much information as you need, but in order to be as clear as possible, here’s how the function works:

  • After receiving the slug and the optional post type, the function initializes the value that will eventually be returned as the permalink. Initializing it to null makes it easier for external functions to determine if the functioned returned a value or a not.
  • Next, an array of arguments is created and merged with the post type if it’s been specified
  • An instance of WP_Query is initialized and executed. If a result is found, the permalink is returned; otherwise, null is returned.

Simple enough, right?

From an external perspective, you could call this function like this:

if( null != ( $permalink = example_get_permalink_by_slug( 'my-example-slug' ) ) {
   // Do work with $permalink
}

Or like this:

if( null != ( $permalink = example_get_permalink_by_slug( 'my-example-slug', 'article' ) ) {
   // Do work with $permalink
}

And that’s all there is to it.