Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Custom Type permalink containing Taxonomy slug

I'm trying to create a permalink pattern for a Custom Type, that includes one of its taxonomies. The taxonomy name is known from the start (so I'm not trying to add or mix all of its taxonomies, just a specific one), but the value will by dynamic, of course.

Normally, the Custom Type permalink is built using the rewrite arg with the slug param, but I don't see how I could add a dynamic variable in there.

http://codex.wordpress.org/Function_Reference/register_post_type

I'm guessing a custom solution is required, but I'm not sure what the best unintrusive approach would be.

Is there a known practice for this or has anyone built something similar recently? I'm using WP 3.2.1 btw.

like image 876
treznik Avatar asked Oct 11 '11 08:10

treznik


People also ask

How do I add custom taxonomy to custom post type?

' So make sure you have a custom post type created before you begin creating your taxonomies. Next, go to CPT UI » Add/Edit Taxonomies menu item in the WordPress admin area to create your first taxonomy. On this screen, you will need to do the following: Create your taxonomy slug (this will go in your URL)

How do I change a permalink slug in WordPress?

Change the Slug for an Individual Post or Page Simply click on Edit, and then change the end of the post URL to whatever you'd like. In the Block Editor, make sure you're in the Document tab in the right-hand sidebar, and then look for the Permalink section. There you can type in your desired slug.

How do I create a custom post type slug in WordPress?

For more details, see our step-by-step guide on how to install a WordPress plugin. Upon activation, you need to go to CPT UI » Add / Edit Post Types to create a new custom post type. You should be on the 'Add New Post Type' tab. First, you need to provide a slug for your custom post type, such as 'movies'.

Can I use the same slug for custom taxonomy and post type?

It is fairly common that you, as a website manager, would like to use the same slug for custom post type as for custom taxonomy, but it is not a simple thing to do if you are not familiar with WordPress permalink structures.

How to create a custom taxonomy for a category url?

Just like category URLs by default look like /category/foo/bar, where foo is the category term and bar is the post name. “category” is the base parameter. Your taxonomy needs its own, such as “campaign”, or any other string that represents a unique URL parameter. Please use cpt code and custom taxonomy code in functions.php

Why does my taxonomy url need a base parameter?

Your taxonomy URL really needs a base parameter so your rewrite rule can detect appropriate requests, otherwise it grabs other requests like simple page requests and mishandles them. Just like category URLs by default look like /category/foo/bar, where foo is the category term and bar is the post name. “category” is the base parameter.


1 Answers

After more searching I managed to create fairly elegant solution using the custom_post_link filter.

Let's say you have a project Custom Type with a client Taxonomy. Add this hook:

function custom_post_link($post_link, $id = 0)
{
  $post = get_post($id);

  if(!is_object($post) || $post->post_type != 'project')
  {
    return $post_link;
  }
  $client = 'misc';

  if($terms = wp_get_object_terms($post->ID, 'client'))
  {
    $client = $terms[0]->slug;

    //Replace the query var surrounded by % with the slug of 
    //the first taxonomy it belongs to.
    return str_replace('%client%', $client, $post_link);
  }

  //If all else fails, just return the $post_link.
  return $post_link;
}

add_filter('post_type_link', 'custom_post_link', 1, 3);

Then, when registering the Custom Type, set the rewrite arg like this:

'rewrite' => array('slug' => '%client%')

I guess I should have dug deeper before asking, but at least we have a complete solution now.

like image 170
treznik Avatar answered Sep 22 '22 01:09

treznik