How to Append/Prepend Permalink with Meta Values in Custom Post Types
One of the most common challenges faced by WordPress developers is creating a custom URL structure for their website's content. This is particularly true when working with custom post types, where the default permalink structure may not align with your desired URL format.
In this blog post, we'll explore a solution that allows you to append or prepend metadata values to the permalink of your custom post types. This technique can be used to create more descriptive, SEO-friendly URLs like www.abc.com/2018/04/best-car-this-year_HONDA
.
Understanding the Challenge
WordPress provides a flexible permalink structure that can be customized to fit your needs. However, when working with custom post types, the default permalink structure may not be sufficient. For example, let's say you have a custom post type called "Detail" and you want the URL structure to include the post's publication year and month, as well as a unique identifier based on a custom field.
The desired URL structure might look like this: www.abc.com/2018/04/best-car-this-year_HONDA
To achieve this, we need to find a way to incorporate the publication date and a custom field value into the permalink.
The Solution
The solution we'll explore involves the use of two WordPress hooks: init
and post_type_link
. Here's how it works:
-
Register the Custom Post Type:
In the init
action, we'll register the custom post type "Detail" with the desired URL structure. We'll use the %detail_year%
and %detail_monthnum%
placeholders to represent the year and month of the post's publication date.
add_action('init', 'Theme2035_detail_register');
function Theme2035_detail_register() {
$ldate = '/%detail_year%/%detail_monthnum%';
$labels = array(
'add_new' => __('Add New Detail', 'espresso'),
'name' => __('Detail','espresso'),
// ... other labels
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'rewrite' => array( 'slug' => $ldate, 'with_front' => false),
'supports' => array('title', 'editor','thumbnail','comments'),
'has_archive' => true,
'taxonomies' => array('post_tag'),
'menu_icon' => 'dashicons-format-office',
);
register_post_type( 'detail' , $args );
}
-
Construct the Permalink:
Next, we'll use the post_type_link
filter to modify the permalink structure. We'll first check if the placeholders %detail_year%
and %detail_monthnum%
are present in the permalink. If they are, we'll replace them with the actual year and month values from the post's publication date.
add_filter('post_type_link', 'ww_construct_permalink', 1, 2);
function ww_construct_permalink( $permalink, $post ){
$year_mask = '%detail_year%';
$month_mask = '%detail_monthnum%';
if( strpos($permalink, $year_mask) === false && strpos($permalink, $month_mask) === false ) {
return $permalink;
}
$year = date("Y", strtotime($post->post_date));
$month = date("m", strtotime($post->post_date));
return str_replace([$year_mask,$month_mask], [$year, $month], $permalink );
}
In this example, the ww_construct_permalink
function checks if the $year_mask
and $month_mask
placeholders are present in the permalink. If they are, it retrieves the year and month values from the post's publication date and replaces the placeholders with the actual values.
Extending the Solution
Now that you have the basic solution in place, you can further enhance it to include additional metadata values in the permalink. For example, you could append a custom field value, such as the "best car this year" as shown in the initial example:
$ldate = '/%detail_year%/%detail_monthnum%_%custom_field%';
To do this, you would need to modify the ww_construct_permalink
function to also retrieve the value of the custom field and include it in the final permalink.
function ww_construct_permalink( $permalink, $post ){
$year_mask = '%detail_year%';
$month_mask = '%detail_monthnum%';
$custom_field_mask = '%custom_field%';
if( strpos($permalink, $year_mask) === false && strpos($permalink, $month_mask) === false && strpos($permalink, $custom_field_mask) === false ) {
return $permalink;
}
$year = date("Y", strtotime($post->post_date));
$month = date("m", strtotime($post->post_date));
$custom_field_value = get_post_meta($post->ID, 'custom_field', true);
return str_replace([$year_mask,$month_mask,$custom_field_mask], [$year, $month, $custom_field_value], $permalink );
}
In this updated example, we've added the %custom_field%
placeholder and the corresponding logic to retrieve the value of the "custom_field" custom field and include it in the final permalink.
Conclusion
By using the init
and post_type_link
hooks, you can easily customize the URL structure of your WordPress custom post types. This solution allows you to append or prepend metadata values, such as publication date and custom field values, to create more descriptive and SEO-friendly URLs.
Remember to flush your permalink settings after implementing these changes by going to the WordPress dashboard, navigating to Permalinks, and clicking the "Save Changes" button.
Flowpoint.ai can help you identify all the technical errors that are impacting conversion rates on your website and directly generate recommendations to fix them, including optimizing your permalink structure for better SEO and user experience.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.