Custom Rewrite Rule for Custom Post Type with URL Param

Question

I am trying to create a custom rewrite rule for a custom post type where

This:

http://test.loc/products/directory/?c=some-value

Should become this:

http://test.loc/products/directory/some-value

Minimal code:

function create_post_type() {
  register_post_type( 'acme_product',
    array(
      'labels' => array(
        'name' => __( 'Products' ),
        'singular_name' => __( 'Product' )
      ),
      'show_ui' => false,
      'public' => true,
      'has_archive' => true,
      'rewrite' => array("slug" => "products/directory")
    )
  );
  add_rewrite_rule( 
      '^products/directory/([A-Za-z0-9-]+)/?$',
      'index.php?post_type=acme_product&c=$1',
      'top'
  );
}

I also tried like this:

add_rewrite_rule( 
    '^products/directory/?([^/]*)/?',
    'index.php?post_type=acme_product&c=$matches[1]',
    'top'
);

I simply can’t get the value of ‘c’. Is there something in the rewrite rule that should be different? I can’t seem to get this to work.

By the way I CAN retrieve ‘some-value’ of ‘c’ from this:

 http://test.loc/index.php?post_type=acme_product&c=some-value

To retrieve the value in the template I tried both:

get_query_var( 'c' );

and

$_GET['c'];

I get nothing back…

0
, , GRowing 4 years 2020-02-22T08:38:36-05:00 0 Answers 80 views 0

Leave an answer

Browse
Browse