I have a custom post type called venues currently they display through the url format:
http://mywebsite.com/venue/{venue-name}
I would like to have the format:
http://mywebsite.com/venue/{venue-suburb}/{venue-name}
But the suburb doesn't actually have any relation to pulling the venue custom post type. Names are always unique.
What would need to happen is at the time of rewrite, it pulls the {venue-suburb} based on the {venue-name} or id and rewrites the url with that information in it.
Is this something that is possible and if so how might I go about starting it?
Update: Here's the code I've been able to put together after some research. The result: http://mywebsite.com/my-venues/{venue-suburb}/{venue-name}/{post_id}
add_action('init', 'myposttype_rewrite');
function myposttype_rewrite() {
global $wp_rewrite;
$queryarg = 'post_type=venue&p=';
$wp_rewrite->add_rewrite_tag('%cpt_id%', '([^/]+)', $queryarg);
$wp_rewrite->add_rewrite_tag('%suburb%', '([^/]+)', $queryarg);
$wp_rewrite->add_rewrite_tag('%title%', '([^/]+)', $queryarg);
$wp_rewrite->add_permastruct('venue', '/my-venues/%suburb%/%title%/%cpt_id%', false);
}
add_filter('post_type_link', 'venue_permalink', 1, 3);
function venue_permalink($post_link, $id = 0, $leavename) {
global $wp_rewrite;
global $wpdb;
$post = &get_post($id);
if ( is_wp_error( $post ) )
return $post;
$newlink = $wp_rewrite->get_extra_permastruct('venue');
$sql = "
SELECT meta_value
FROM " . $wpdb->postmeta . "
WHERE post_id = " . $post->ID . " AND meta_key = 'address'";
$address = $wpdb->get_var( $sql );
$address = unserialize( $address );
$suburb = str_replace(" ", "-", strtolower($address['suburb']));
$title = str_replace(" ", "-", strtolower(get_the_title( $post->ID )));
$newlink = str_replace("%suburb%", $suburb, $newlink);
$newlink = str_replace("%title%", $title, $newlink);
$newlink = str_replace("%cpt_id%", $post->ID, $newlink);
$newlink = home_url(user_trailingslashit($newlink));
return $newlink;
}
Removing the section for %cpt_id% makes the page come up as a 404. Rewriting %cpt_id% to "" also makes the page come up as a 404. Anyone know how to remove the post_id?