1

I added the code below and successfully changed my CPT slug for the taxonomy terms. My blog permalinks are /article/%postname% in case you wonder, and I'd like to keep it that way, so no clashes with CPTs.

It works fine and I got rid of the CPT slug in the single CPT's permalink ("listings").

function listing_type_link_filter_function( $post_link, $id = 0, $leavename = FALSE ) {
    if ( strpos('%listing_type%', $post_link) === 'FALSE' ) {
      return $post_link;
    }
    $post = get_post($id);
    if ( !is_object($post) || $post->post_type != 'listing' ) {
      return $post_link;
    }
    $terms = wp_get_object_terms($post->ID, 'listing_type');
    if ( !$terms ) {
      return str_replace('/listing_type', '/', $post_link);
    }
    return str_replace('%listing_type%', $terms[0]->slug, $post_link);
  }

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

It works fine, and instead of:

mysite.com/listing_types/tours/tour-name (CPT "listing" post)

those posts show as:

mysite.com/tours/tour-name

and they are found correctly for any of the following

mysite.com/listing_type/tours/tour-name
mysite.com/listings/tours/tour-name
mysite.com/listing_types/tours/tour-name
mysite.com/listing/tours/tour-name

Now I need to hook the same or similar filter to the term archive links filter, which I can't find, if there's any.

so instead of

mysite.com/cpt_slug/taxonomy_term (archive page)

I'd get

mysite.com/taxonomy_term (archive page)

…because the most important things in my site are those terms: listing_types, (tours, attractions, events) but I can't let the "listings" or "listing_types" slugs to show, even when they ARE listings for me.

Do you know what hook and how should I filter it to replace the listing_type slug from the TERM ARCHIVE pages link?

Or is it easier to set the general permalinks settings page as %term%/%postname% and make the CPTs to use it, and customizing the regular posts instead? –since most of defaults could benefit to the CPTs and the posts have no terms.

Thanks.

5
  • My father used to say "the simple action of writing something, makes the thumb to trigger the inspirational "click" in the brain, that resolves the problem".
    – sergio
    Commented Jul 2, 2012 at 23:55
  • 2
    Add your solution as a real answer and mark the question as answered.
    – fuxia
    Commented Jul 3, 2012 at 0:25
  • Oh, now I can answer my own questions! :) Nice.
    – sergio
    Commented Jul 23, 2012 at 12:29
  • I posted the issue with the solution i found out myself, but… now that I found out errors, Can I post it as a separated question?
    – sergio
    Commented Jul 23, 2012 at 12:48
  • 1
    Post the solution that works as an answer (the big textarea with Your Answer as headline), not as an update to the question. If you have a new problem ask a new question that should work standalone.
    – fuxia
    Commented Jul 23, 2012 at 12:52

1 Answer 1

2

This worked fine for me, except the whole system above has some issue with my configuration (I made my wp installation Multisite) that broke all the other posts. They are all not-found, although the permalink is retrieved correctly every time, as it was found.

I'm opening a new question for that other issue here so you can also use the code above safely.

  function term_link_filter_function( $termlink, $term, $taxonomy ) {
    if ( strpos('listing_type', $termlink) === 'FALSE' ) {
      return $termlink;
    } else  return str_replace('/listing_type', '', $termlink);
  }

add_filter('term_link', 'term_link_filter_function', 1, 3);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.