Rewrite Rule Working for all but one of the taxonomies created

Question

I have a hierarchical taxonomy, county, which takes a parent and child taxonomy as well as a rewrite rule to include them in my permalinks.

I have three of these county taxonomies, but only two of them are working as expected. For the third one, the permalinks break when you add both the parent and child taxonomy, but work correctly if you only add the child taxonomy. If you add both parent and child taxonomy to this specific taxonomy, only the parent shows up in the permalink. I’ve narrowed it down to it being an issue with that taxonomy’s specific child taxonomy, because I added a test child taxonomy and that one worked fine as well.

What’s weird is that for the broken permalinks, if you manually add the child taxonomy to the permalink in the address bar, it will take you to the correct page. However, the permalinks created are 404s

What are the potential reasons for rewrite rules not working on a specific child taxonomy? My only guess is some leftover data from when I maybe created a county by the same name before my current setup, but I don’t know where to look in the database for that sort of thing.

For reference, my rewrite functions:

<?php


add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules)
{
    $newRules  = array();
    $newRules['floorplans/(.+)/(.+)/(.+)/?$'] = 'index.php?floorplan=$matches[3]';
    $newRules['ready-made/(.+)/(.+)/(.+)/?$'] = 'index.php?ready_made=$matches[3]';

    $newRules['floorplans/(.+)/(.+)/?$'] = 'index.php?county=$matches[2]';
    $newRules['ready-made/(.+)/(.+)/?$'] = 'index.php?county=$matches[2]';

 
    $newRules['floorplans/(.+)/?$']                = 'index.php?county=$matches[1]';
    $newRules['ready-made/(.+)/?$']                = 'index.php?county=$matches[1]';
    return array_merge($newRules, $rules);
}

function filter_post_type_link($link, $post)
{
    $commcheck = get_the_terms($post->ID, 'county');
    if ($post->post_type == 'community') {
        $link = str_replace('%county%', array_pop($commcheck)->slug, $link);
    }
    if ($post->post_type == 'floorplan' || $post->post_type == 'ready_made') {
        if ($cats = get_the_terms($post->ID, 'county')) {
            $link = str_replace('%county%', get_taxonomy_parents(array_pop($cats)->term_id, 'county', false, '/', true), $link); // see custom function defined below
        }
    }
    return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

// my own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = true, $visited = array())
{
    $chain = '';
    $parentref = get_term($id, $taxonomy);
    $parent = &$parentref;

    if (is_wp_error($parent)) {
        return $parent;
    }

    if ($nicename)
        $name = $parent->slug;
    else
        $name = $parent->name;

    if ($parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited)) {
        $visited[] = $parent->parent;
        $chain .= get_taxonomy_parents($parent->parent, $taxonomy, $link, $separator, $nicename, $visited);
    }

    if ($link) {
        // nothing, can't get this working :(
    } else
        $chain .= $name . $separator;
    return $chain;
}

0
, , itsseanl 1 year 2020-07-20T04:10:37-05:00 0 Answers 54 views 0

Leave an answer

Browse
Browse