As we know, the Rewrite API in WordPress, allows us to control the structure and handling of URLs for our content. Whether we want to create custom URLs for our post types, taxonomies, or even specific pages, this API comes in handy. In this blog post, we will walk through how to leverage the Rewrite API to create custom permalink structures for a custom post type and its taxonomy.
Adding Custom Rewrite Rules
We have a custom post type called my_event
, which is used to represent different events. We also have a custom taxonomy called event_type
that categorizes the events. We will set up custom permalinks to display events in a structured format, including both the event type and a unique identifier for the event.
First, we need to tell WordPress how to interpret custom URLs for our events. In the code snippet below, we define a custom rewrite rule that includes both the event type and a unique event identifier in the URL.
function event_rewrite_rule() { // Add rewrite tag for event type. add_rewrite_tag('%event_type%', '([^&]+)', 'event_type='); // Add custom rewrite rule. add_rewrite_rule( '^event/([^/]+)/([^/]+)-([0-9]+)/?', 'index.php?event_type=$matches[1]&post_type=my_event&name=$matches[2]&p=$matches[3]', 'top' ); } add_action('init', 'event_rewrite_rule');
Customizing Permalink Structure for Events
We need to customize the permalink structure for the my_event
post type to include both the event type and the event ID.
function custom_event_permalink() { global $wp_rewrite; $args = $wp_rewrite->extra_permastructs['my_event']; // Remove the default permalink structure remove_permastruct('my_event'); unset($args['struct']); $args['with_front'] = false; // Define the new permalink structure. add_permastruct('my_event', 'event/%event_type%/%my_event%-%post_id%', $args); } add_action('init', 'custom_event_permalink', 11); // 11 ensures this runs after other rules.
Modifying Permalink for Each Event
To ensure that each event’s permalink reflects its event type, we need to modify the URL generation process for individual events.
function event_permalink($permalink, $post) { if ('my_event' !== $post->post_type) { return $permalink; } $type = wp_get_object_terms($post->ID, 'event-type'); $type = wp_list_sort($type, array('term_id' => 'ASC')); if (!is_wp_error($type) && !empty($type) && is_object($type[0])) { $type_slug = $type[0]->slug; } else { $type_slug = 'hello'; // Default slug if no event type is found. } // Replace placeholders with values. $permalink = str_replace( ['%event_type%', '%post_id%'], [$type_slug, $post->ID], $permalink ); return $permalink; } add_filter('post_type_link', 'event_permalink', 10, 2);
Filtering Events by Event Type
Finally, we want to make sure that WordPress can filter events by the event_type
taxonomy on the front end and in the admin panel. This will allow for custom filtering based on the event type slug in the URL.
function add_event_type_query_var($vars) { $vars[] = 'event_type'; return $vars; } add_filter('query_vars', 'add_event_type_query_var'); function filter_events_by_event_type($query) { if (is_admin() && !$query->is_main_query()) { return; } $event_type = get_query_var('event_type'); if (!empty($event_type)) { $tax_query = $query->get('tax_query'); if (!is_array($tax_query)) { $tax_query = array(); } $tax_query['relation'] = 'AND'; $tax_query[] = array( 'taxonomy' => 'event-type', 'field' => 'slug', 'terms' => $event_type, ); $query->set('tax_query', $tax_query); } } add_action('pre_get_posts', 'filter_events_by_event_type');
Conclusion
With this, using the WordPress Rewrite API, we have successfully created a custom URL structure for our my_event
post type and filtered events based on their event_type
taxonomy. This provides a clean, SEO-friendly URL structure for our custom events and ensures the filtering works seamlessly on both the front end and back end.
Leave a Reply