0

I would like to simply create an url rewrite that takes the third argument and matches it to its corresponding pages, ignoring the other two arguments.

domain.com/argument1/argument2/page-slug = 'index.php?page_id=XXX';

Is it possible to call a page via it's slug instead of it's ID, or can I somehow generate the id, based on a pages slug?

UPDATE:

I actually got a lot further here now:

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');

function mmp_rewrite_rules($rules) {
   $newRules  = array();
   $newRules['bus-tours/(.+)/(.+)/(.+)/?$'] = 'index.php?bus-tours=$matches[3]';
   $newRules['(.+)/?$']                     = 'index.php?pagename=$matches[1]';
   //$newRules['bus-tours/(.+)/(.+)/?$']    = 'index.php?pagename=$matches[2]';

return array_merge($newRules, $rules);
}

What this allows me is:

/bus-tours -> and it shows the bus-tours pages.
/bus-tours/California -> and it shows me the child page of bus-tours, which is Calfironia
/bus-tours/california/SanFran/Tourx -> and it shows me the CPT post for TourX

So, pretty good for now. Tho, since I am using a lot of CPT's I would like to modify the first rule so "bus-tours" can be replaced with a variable.

$newRules['(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?$matches[1]=$matches[3]';

This doesn't work. Any help here?

Thanks, Daniel

2 Answers 2

2

Use pagename instead of page_id to query by slug. Note that if a page is a child of another page, pagename has to be the full parent/child path to the page, since slugs only have to be unique per-level in hierarchical post types. See WP_Query for the full list of query vars.

0

You can try selecting the $post_id where $page_name = $page_name

1
  • 2
    Please expand upon this answer. As is, it is not very useful for anyone who doesn't already know what to do
    – s_ha_dum
    Commented May 21, 2015 at 16:26

Your Answer

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

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