1

I'm working on a project which involves filtering some customs posts on the front-end. I managed to create the permalinks including a custom field, but it seems that the rewrite rule matches some unwanted pages, making them not display-able on the frontend.

The code I use for rewriting :

$wp_rewrite->add_rewrite_tag('%competition%', '([^/]+)', 'competition=');
$wp_rewrite->add_rewrite_tag('%prize_category%', '([^/]+)', 'prize_category=');
$wp_rewrite->add_permastruct('competition', '/au/%prize_category%/%competition%/', false);

The problem is the following. It does what it should do for this url:

http://example.com/au/win-a-holiday/win-a-long-weekend-at-the-rees-hotel-queenstown/

But it also matches this one:

http://example.com/au/win-a-holiday/

Is there anyway I could force the second parameter %competition% to be mandatory? So the rule will apply only for URLs like:

http://example.com/au/param1/param2 not for domain.com/au/param1/ ?

Thanks, George

1

I've been struggling all morning to solve this and found your question when searching for the answer. I'm sure it's a little late for you now but in case anyone else stumbles across this question here's the solution I found:

Change:

$wp_rewrite->add_permastruct('competition', '/au/%prize_category%/%competition%/', false); 

To:

$wp_rewrite->add_permastruct( 'competition', '/au/%prize_category%/%competition%/', array( 'walk_dirs' => false ) );

walk_dirs rewrites the "directories" that make up the URL structure and is set to true by default. Setting it to false ensures the rewrite is only used for the full URL specified.

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.