Wordpress - get link to a post
Categories:
Mastering WordPress Post Links: A Comprehensive Guide

Learn how to programmatically retrieve and display post links in WordPress using various PHP functions, ensuring flexibility and control over your content presentation.
Retrieving the link to a WordPress post is a fundamental task for developers and theme designers. Whether you need to display a permalink within a custom loop, create a 'Read More' button, or integrate with external systems, WordPress provides several robust functions to achieve this. This article will explore the most common and effective methods for getting a post's link, along with practical examples and best practices.
Understanding WordPress Post Links
WordPress uses a permalink structure to generate user-friendly and SEO-optimized URLs for posts, pages, categories, and other content types. When you ask for a post's link, you're typically requesting its permalink. The method you choose depends on the context: are you inside The Loop, or do you need the link for a specific post ID outside of it?
flowchart TD A[Start] --> B{Inside The Loop?} B -- Yes --> C[Use the_permalink() or get_permalink() without arguments] B -- No --> D{Have Post ID?} D -- Yes --> E[Use get_permalink( $post_id )] D -- No --> F[Query for Post ID first] C --> G[Output Link] E --> G F --> E G --> H[End]
Decision flow for retrieving a WordPress post link.
Methods for Retrieving Post Links
WordPress offers several functions, each suited for different scenarios. The primary functions are the_permalink()
and get_permalink()
. Understanding their differences is key to using them effectively.
<?php
// 1. Inside The Loop (displays the link directly)
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
// Or simply: the_permalink(); if you just need the URL printed
}
}
// 2. Outside The Loop, for a specific post ID
$post_id = 123; // Replace with your desired post ID
$post_link = get_permalink( $post_id );
if ( $post_link ) {
echo '<p>Link to post ID ' . $post_id . ': <a href="' . esc_url( $post_link ) . '">View Post</a></p>';
}
// 3. Getting the link for the current post outside The Loop (e.g., in a header/footer)
// This works if a global $post object is available, which it often is.
global $post;
if ( $post ) {
$current_post_link = get_permalink( $post->ID );
echo '<p>Link to current post: <a href="' . esc_url( $current_post_link ) . '">Current Post</a></p>';
}
// 4. Using get_post_field for the post_name (slug) and constructing the URL
// This is generally less reliable than get_permalink() but can be useful in specific cases.
$post_slug = get_post_field( 'post_name', $post_id );
$base_url = home_url(); // Or get_site_url()
$constructed_link = $base_url . '/' . $post_slug . '/'; // This assumes default permalink structure
// Note: This method is NOT recommended for general use as it doesn't account for custom permalink structures.
?>
Various PHP methods to retrieve WordPress post links.
get_permalink()
over the_permalink()
if you need to store the link in a variable, manipulate it, or pass it to another function. the_permalink()
directly echoes the URL, which is less flexible.Advanced Scenarios and Best Practices
Beyond basic retrieval, there are considerations for security, performance, and specific use cases like custom post types or external links.
esc_url()
to prevent XSS vulnerabilities. This is crucial for security, especially if the URL might originate from user input or external sources.Getting Links for Custom Post Types
The get_permalink()
function works seamlessly with custom post types (CPTs) as well. As long as your CPT is registered with public => true
and rewrite => true
, WordPress will generate permalinks for it, and get_permalink()
will retrieve them just like regular posts.
<?php
// Example for a custom post type 'product'
$product_id = 456; // Replace with your product ID
$product_link = get_permalink( $product_id );
if ( $product_link ) {
echo '<p>Link to product ID ' . $product_id . ': <a href="' . esc_url( $product_link ) . '">View Product</a></p>';
}
?>
Retrieving a link for a custom post type.
Filtering Permalinks
WordPress provides filters that allow you to modify permalinks before they are returned. The post_link
filter is particularly useful for this. You might use it to add tracking parameters, change the domain for specific posts, or integrate with a CDN.
<?php
function my_custom_permalink_filter( $permalink, $post, $leavename ) {
// Example: Add a tracking parameter to all post links
if ( ! is_admin() && 'post' == get_post_type( $post ) ) {
$permalink = add_query_arg( 'source', 'mywebsite', $permalink );
}
return $permalink;
}
add_filter( 'post_link', 'my_custom_permalink_filter', 10, 3 );
?>
Using the post_link
filter to modify permalinks.
add_query_arg()
and remove_query_arg()
which safely handle query parameters.