0

I took over a WordPress project from another programmer, who wrote code to create the permalink structure "custom-taxonomy-slug/cpt-slug", the custom taxonomy is "city", the cpt is "restaurant", so each restaurant gets one city as category, so the restaurant "Big Burgers" in Berlin would have the permalink "berlin/-big-burgers". This works, but the problem is that his code leads to 404 errors for normal subpages (page-name/child-page-name) and also for blog pagination (blog/page/2). Here's the main part in functions.php that is responsible for this issue:

function entry_rewrite_rules() {

    $custom_structure = '/%city%/%restaurant%';

    add_rewrite_tag( "%restaurant%", '([^/]+)', "restaurant=" );

    add_permastruct( 'restaurant', $custom_structure, array( 'walk_dirs' => false ) );

}

add_action( "init", "entry_rewrite_rules" );

There is another part, which is just manipulating the permalink for the cpt restaurant, but is not the part that leads to the 404 errors.

function change_entry_permalink( $url, $post ) {

    if ( get_post_type( $post ) === 'restaurant' ) {

        $terms = wp_get_object_terms( $post->ID, 'city' );

        if ( $terms ) {

            return str_replace( '%city%', $terms[0]->slug, $url );

        }

    }

    return $url;

}

add_filter( 'post_type_link', 'change_entry_permalink', 10, 2 );

As far as I understand, the problem is that the other programmer didn't add a permalink base to his custom link structure, e.g. "entries/%city%/%restaurant%", and so all page/subpage links are handled as restaurant links, and will lead to 404 errors if they are not links to a restaurant entry.

The site has been online for many years, and there are nearly 4000 restaurants, so changing the permalinks for the restaurants (and adding redirects) is probably not a good option. (4000 redirects might be bad for performance and SEO.)

So my questions are:

  1. How can I get pagination for the blog working the normal way, e.g. blog/page/2? (Since /blog works, I know I could program my own pagination logic with e.g. /blog?page=2, but I would prefer not to.

  2. While I generally can live without subpages, it would still be nice to have the ability to makes specific subpages work, even if it means to add some code for every permalink, e.g. my-page/my-important-subpage

I was thinking about somehow changing the regex in this part in a way that it excludes blog/page/2 or other specific permalinks:

add_rewrite_tag( "%restaurant%", '([^/]+)', "restaurant=" );

Unfortunately, everything I tried failed, because I don't fully understand what is happening there, and I'm not good at creating regex code.

My other idea was to change the priority argument for adding the rewrite rules, but neither setting it to 1 or to 99 worked:

add_action( "init", "entry_rewrite_rules", 1); // or 99 or 20...

As I said, getting blog/page/2 to work is my priority, if you have any other ideas for restoring the "normal" capabilty to have subpages, this would also be great.

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.