1

I've been having looot of troubles with this function, I don't know what to do, because I tried everything.

I have a custom post type, let's call it study. The url structure for it is

http://example.com/study/studyname

They have different sections, I want to have the new structure like this:

http://example.com.com/study/studyname/view/view_arg

So far I've tried add_rewrite_endpoint() and add_rewrite_rule() functions, with any result.

I also tried to disable all my plugins just in case one of them may be the reason, but still fails. This is my code, I tried both options separately:

add_filter('init', 'add_rewrite_rules');
function add_rewrite_rules() {
    global $wp_rewrite;
    add_rewrite_endpoint('view', EP_PERMALINK | EP_PAGES, 'view' );
    $wp_rewrite->flush_rules();
}

Here I also test it with EP_ALL, without the view, without flushing rules...

Also I tried to manually create the rewrite rule:

add_rewrite_rule(
    'study/([^/]+)/view/([^/]+)?$',
    'index.php?study=$matches[1]&view=$matches[2]',
    'top'
);

And I read that the query_var should be used configured as well, so I did:

add_filter( 'query_vars', 'conf_query_vars' );
function conf_query_vars($query_vars){
    $query_vars[] = 'view';
    return $query_vars;
}

I'm going to kill myself, if I try the url with the parameter itself (something like http://example.com/study/studyname/?view=view_args) it works like a charm, but I don't want to show this structure.

  • 1
    What doesn't work exactly? You get a 404? – Milo Nov 4 '14 at 14:29
  • 4
    »I'm going to kill myself«...don't do that – Nicolai Nov 4 '14 at 14:31
  • 1
    Yes, the error is a 404. And the kill myself was just and expression xD – Fernando Vera Nov 5 '14 at 14:55
  • Not seen anyone else mention this, so are you updating the Permalinks again after a code update? I did some rewrite changes before which didn't work until I went into Permalinks and clicked Save Changes. – TomC Aug 10 '16 at 15:52
1

Your code should (and does from quick check) work in principle. You should not be flushing permalink every page load, just once.

Use rewrite helper like Monkeyman Rewrite Analyzer to see if rules are being generated, what they are, and how are they matching to real URLs in your site.

  • I know, and I don't understand why. Maybe I'll use that tool you told me just to know it, I gave up because I spent a loot of hours with this and I decided that it didn't worth the time any more, so I make a turnaround with this. Still, thank you very much :) – Fernando Vera Nov 5 '14 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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