0

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.

2 Answers 2

0

Numbers work because single posts can have pagination, and there is a rewrite rule to support that.

If you want to add an additional parameter after a permalink, add a rewrite endpoint.

add_rewrite_endpoint( 'show', EP_PERMALINK );

EDIT- Here's an internal rewrite example, assuming your custom post type slug is type1:

add_rewrite_rule(
    'c/([^/]+)/([^/]+)/([^/]+)/?$',
    'index.php?type1=$matches[1]&type2=$matches[3]',
    'top'
);
5
  • My problem is that the endpoint text can change. show was just an example. But it could be different for each of the posts and also the post can have more than one endpoint. I can register the endpoints as I save the post but I dont think that would be good (unless there is a way to register an endpoint per post).
    – kks21199
    Apr 22, 2017 at 16:29
  • Can you explain your rewrite rule and the structure of your CPT? I think you would greatly simplify things if you make your rewrite rules internal.
    – Milo
    Apr 22, 2017 at 16:35
  • Here is my previous question, wordpress.stackexchange.com/questions/263988/… . My custom post type is non-hierarchical and has a custom taxonomy in its slug ( slug => 'c/%taxonomy%')
    – kks21199
    Apr 22, 2017 at 17:10
  • I would have solved that with an internal rewrite. See edit above.
    – Milo
    Apr 22, 2017 at 17:39
  • Thanks :) I just came to that solution a while ago. Though in my case its index.php?post_type=cpt&name=$matches[2]&type2=$matches[3]. And I also added ` add_rewrite_tag('%type2%','([^&]+)');`.
    – kks21199
    Apr 22, 2017 at 17:42
0

It gives you 404 by a fact that there is nothing found on your request. I guess it's because type is registered query_var. You can try to debug this situation with plugins: debug-bar (tab "Request") and debug-bar-rewrite-rules (Rewrite Rules).

2
  • but how does using numbers at the end work? I tried adding a function to die() at init echoing the $_GET variables and I see show or any other text I add in the url. So is there a way to have a function before wordpress gives 404 so I can check if it is a custom post type and make my own logic?
    – kks21199
    Apr 22, 2017 at 14:19
  • Slow down. Do you understand how Rewrite Rules work in Wordpres? Yo create rule for your URL, whe your URL matchs your rule wp receive array of query vars ( ones received from url ), based on vars wp quering posts. Always. if posts not found youll get 404, if posts fond wp will search for a template and pass datat tp it ( using have_posts() etc... IMHO I already tald you whats wrong, you need to prove that using plugins i named. Apr 22, 2017 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.