Trouble with Custom Rewrite Rules in WordPress for Post Types and Categories

74 Views Asked by At

I'm working on a WordPress project where I need to create custom rewrite rules for various post types and their associated categories. The goal is to rewrite URLs in the format /post-type-slug/category/category-name/. However, I'm facing an issue where the post_type query variable is not being set as expected when the rewritten URL is accessed.

Here's the relevant part of my code:

add_action('init', function () {
    $postTypes = get_post_types(['public' => true, '_builtin' => false], 'objects');

    foreach ($postTypes as $postType) {
        add_rewrite_rule(
            "{$postType->rewrite['slug']}/category/(.+)/?$",
            "index.php?post_type=\$matches[1]&category_name=\$matches[2]",
            'top'
        );
    }

    // flush_rewrite_rules(); // Uncommented during testing
});

add_filter('template_include', function ($template) {
    var_dump(get_query_var('post_type')); // For debugging
    die;
    if (is_category() && get_query_var('post_type')) {
        $postType = get_query_var('post_type');
        $customTemplate = locate_template("category-{$postType}.php");

        if ($customTemplate) {
            return $customTemplate;
        }
    }

    return $template;
});

  • Manual Rewrite Rules Flush: I've manually flushed the rewrite rules to ensure that the new rules are recognized by WordPress. This was done by uncommenting flush_rewrite_rules(); in the init action and also by visiting the "Settings > Permalinks" page in the WordPress admin area and saving the settings.

  • Direct Query String Testing: To ensure that the issue was with the rewrite rule and not with query variable retrieval, I directly accessed a URL with query strings like http://example.com/index.php?post_type=news&category_name=media. This approach correctly set and retrieved the post_type query variable.

  • Checking for Plugin/Theme Conflicts: I have tested this with all other plugins disabled and while using a default WordPress theme to rule out conflicts from other sources.

  • Debugging with var_dump: I used var_dump(get_query_var('post_type')); within the template_include filter to debug the value of post_type. It consistently returns an empty string when accessing rewritten URLs, even though it works fine with direct query strings.

  • Reviewing Custom Post Type Settings: I've confirmed that all custom post types involved are set to 'public' => true and '_builtin' => false, and that their rewrite slugs are correctly set up.

  • Examining Global $wp_rewrite Object: I checked the global $wp_rewrite object to confirm that my rewrite rules were being added correctly.

  • Regular Expression Validation: I validated the regular expressions used in add_rewrite_rule to ensure they correctly match the intended URL structure.

Despite these efforts, the issue persists, and I'm unable to retrieve the post_type query variable from the rewritten URL. I'm looking for insights on what I might be missing or any alternative approaches to troubleshoot this further.

0

There are 0 best solutions below