WordPress.org

Ready to get started?Download WordPress

Forums

matches[3] not working (1 post)

  1. shulato
    Member
    Posted 3 years ago #

    what is the problem with this

    add_filter('rewrite_rules_array', 'product_rules');
    add_filter('query_vars', 'add_query_vars');
    add_filter('init', 'flush_rules');

    function flush_rules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
    }

    function product_rules($rules) {
    $new_rules = array(
    'products/(\d+)-.+/?$' => 'index.php?pagename=products&product_id=$matches[1]',
    'products/(.+)/(.+)/?$' => 'index.php?pagename=products&category=$matches[1]&subcategory=$matches[2]',
    'products/(.+)/(.+)/(\d+)-(.+)/?$' => 'index.php?pagename=products&category=$matches[1]&subcategory=$matches[2]&product_id=$matches[3]',
    );
    return $new_rules + $rules;
    }

    function add_query_vars($vars) {
    $vars[] = 'product_id';
    $vars[] = 'category';
    $vars[] = 'subcategory';

    return $vars;
    }

    i could use the 1 and 2 rules but on the 3rd rule

    products/(.+)/(.+)/(\d+)-(.+)

    it doesn't work
    the result is
    $matches[1] = $category/$subcategory
    $matches[2] = $product_id

    which it should be
    $matches[1] = $category
    $matches[2] = $subcategory
    $matches[3] = $product_id

Topic Closed

This topic has been closed to new replies.

About this Topic