1

I made a custom taxonomy and I'm trying to customise the permalink structure for each term by using term custom meta. At the moment I have a custom post type magazine_article and a custom taxonomy magazine.

My aim is to get a structure as follows:

  • /magazines/ archive for magazine_article post type served by archive-magazine_article.php
  • /magazines/15/ for term archive served by taxonomy-magazine.php
  • /magazines/15/linus-interview/ for single magazine_article served by single-magazine_article.php

I got all of them working nicely except for the term archive (second bullet).

As a matter of fact, the term name is something like 'foo bar' which originally outputs a permalink such as '/magazines/foo-bar/' and I have a term custom meta with a value that I need for the rewrite for example '15'

I tried this:

on Class Magazine_Taxonomy

public function edit_term_permalink($url, $term, $taxonomy) {
    if ($taxonomy == self::$mag_taxonomy_slug) {
        $meta = get_term_meta($term->term_id, self::$mag_no_key);
        $mag_no = $meta[0];
        $url1 = explode('magazines/', $url);
        $url2 = $url1[0] . 'magazines/' . $mag_no . '/';
        return $url2;
    } else {
        return $url;
    }
}

Then on my Manager class:

$this->loader->add_filter('term_link', $magazine_taxonomy, 'edit_term_permalink', 10, 3);

When using get_term_link() for a term of the magazine taxonomy I get exactly what I want, even on the backend the terms 'view' links give me the correct link. But the link doesn't work. It seems like if I'm modifying the link that wordpress gives me but without registering it within its rewrite rules.

However, only the original 'magazines/hello-world/' does work.

I went on Settings > Permalinks multiple times, restarted the server, deleted the .htaccess file and nothing seems to work. I ran out of ideas on how to make this work so I guess that the filter term_link does not do what I want. How could I achieve this ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.