Martijn van Hoof Martijn van Hoof - 3 months ago 12
PHP Question

Wordpress CPT extra parameter at the end of the URL -> 404

I've created a Custom Post Type. Collections See code below.

`$labels = array(
'name' => __( 'Collections', 'pcs' ),
'singular_name' => __( 'Collection', 'pcs' ),
'add_new' => __( 'New Collection', 'pcs' ),
'add_new_item' => __( 'New Collection', 'pcs' ),
'edit_item' => __( 'Edit Collection', 'pcs' ),
'new_item' => __( 'New Collection', 'pcs' ),
'view_item' => __( 'View Collection', 'pcs' ),
'search_items' => __( 'Search Collections', 'pcs' ),
'not_found' => __( 'No Collection Found', 'pcs' ),
'not_found_in_trash' => __( 'No Collection Found in Trash', 'pcs' ),
'parent_item_colon' => __( 'Parent Collection', 'pcs' ),
'menu_name' => __( 'All Collections', 'pcs' ),
'filter_items_list' => __( 'Filter collections list', 'pcs' ),
'items_list_navigation' => __( 'Collections list navigation', 'pcs' ),
'items_list' => __( 'Collections list', 'pcs' )
);`

$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'title' ),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => 'pcs',
'menu_position' => 1,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => array(
'slug' => 'collections'),
'with_front' => false
),
'capability_type' => 'post'
);


Works like a charm.
When I go to this page: http://www.example.com/collections/slug

it shows me the template thanks to a hook:

function xload_template( $single_template ) {

global $post;

if ( $post->post_type == 'my_collection' ) {
if ( file_exists( PATH . 'frontend/template.php' ) ) {
$single_template = PATH . 'frontend/template.php';
}
}

return $single_template;
}

add_filter( 'single_template', 'xload_template' );


The problem is, I want to pass an extra variable trough the url like this: http://www.example.com/collection/slug/email_md5

So I can use the email_hash as verification for viewing the collection. I know I have to make a url rewrite rule but I cannot get it working.

function add_rewrite_rules($aRules) {
$aNewRules = array('collections/([^/]+)/([^/]+)/?$' => 'index.php?collections=$matches[1]&email=$matches[2]','top');
$aRules = $aNewRules + $aRules;
return $aRules;
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');


FIX: Since I was quite new to Custom Post Types within plugins from Wordpress I figured out it was much easier to create a rewrite if you know what Wordpress is doing. Change your wordpress settings-> Permalinks -> Plain.
This way you can see how the url is called and how you need to use it within your own rewrites. The Answer below solved it for me. Thanks

Answer

I think what you are looking for is add_rewrite_rule. You can try something like the following.

  function custom_rewrite_rule() {
    add_rewrite_rule('collections/([^/]+)/([^/]+)/?$','index.php?collections=$matches[1]&email=$matches[2]','top');
  }
  add_action('init', 'custom_rewrite_rule', 10, 0);