Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I added my taxonomies in the permalink instead of the Custom Post Type's slug, and it works fine. I'd even say everything was working fine but changing the WP installation to Multisite broke all other posts and pages (not the CPTs).

The permalinks are generated correctly every time as ?p=123 or other guessed urls, so the post info is found, but not found for the loop.

This is how I got the taxonomy in place of the CPT's slug:

1) The post_type rewrite is defined as %listing_type% and it works fine, as follow:

'rewrite' => array( 
            'slug' => '/%listing_type%', 
            'with_front' => false,

2) And the following functions replace the %listing_type% and "listing_type" string from the urls and term links, generated by e.g. wp_list_categories(), respectively (removing the CPT's name from the url)

  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);


  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);

Now, I'm almost certain that everything was working fine before I made it a Multisite. I'll research how to safely un-do it (the multisite thing) and post the update later.

Any idea what could be breaking the posts and pages NOT in the above CPTs? If I remove the %s from the post_type slug, everything works fine.

UPDATAE

I noticed other working examples had a rewrite rule for each custom permalink, so I added it, and VOILA, the posts are fixed now, but the pages are still broken

THIS PARTIALLY WORKS (posts only)

global $wp_rewrite;
$wp_rewrite->extra_permastructs['listing'][0] = "%listing_type%/%postname%";
    add_rewrite_rule("([^/]+)/([^/]+)/?$", 'index.php?post_type=listing&listing_type=$matches[1]&name=$matches[2]', 'bottom');
    add_rewrite_rule("([^/]+)/([^/]+)/?$", 'index.php?post_type=listing&listing_type=$matches[1]&page=$matches[2]', 'bottom');

The above code (inside the CPT definition function) only fixed the posts, but the pages (what the second add_rewrite_rule line was ment for) are still broken.

share|improve this question
add comment

1 Answer

Add your rewrite rules to top, not bottom. Every rule that matches will abort the process. So if there's only a single match before, your rules will get skipped.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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