I use the following method to add a $_GET Variable to my custom post type in wordpress.
add_filter('query_vars', 'add_type2_var', 0, 1);
function add_type2_var($vars){ $vars[] = 'type2'; return $vars; }
add_rewrite_rule('/?c/(.[^/])/(.[^/])/(.*)$',"/c/$1/$2?type2=$3",'top');
My custom post type is non-hierarchical post. The links to the posts works without a problem but when I try to add something at the end after the slash the links stop working.
If I add show
at the end of http://127.0.0.1/wp/c/default/dd
http://127.0.0.1/wp/c/default/dd/show
the link gives a 404 error. Though it works perfectly fine when I add numbers at the end like http://127.0.0.1/wp/c/default/dd/123456
and I could also get the 123456
using $_GET
in php.
I am not sure where the problem is but my guess is that wordpress gives an 404 error before my function. I have a function using single_template
filter.
UPDATE:
I have added a function to `template_redirect.
function CPTTest()
{
if ( is_singular('cpt') )
{
echo 'cpt';
}
elseif ( is_singular() ) {
echo 'post';
}
else
{
echo 'none';
}
var_dump($_GET);
die();
}
This works fine when I use the url http://127.0.0.1/wp/c/default/dd/show
. Only problem is that it doesn't recognize it as a cpt post. But it does recognize http://127.0.0.1/wp/c/default/dd/123456
as a cpt post. Same result when I used wp
action instead of template_redirect
.