Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for a wordpress rewrite that will let me pass a query var. I have a custom post type that produces urls like this:

example.com/custom-post-slug/custom-post-title/

my goal is to have

example.com/custom-post-slug/custom-post-title/cid/10/

Pass the value 10 into cid

Any suggestions?

share|improve this question

3 Answers 3

You can use add_rewrite_endpoint, which will add a query var and generate the necessary rewrite rules:

function wpd_cid_endpoint() {
    add_rewrite_endpoint( 'cid', EP_PERMALINK );
}
add_action( 'init', 'wpd_cid_endpoint' );

Don't forget to flush rewrite rules after changing rules. You can do this quickly by visiting the Settings > Permalinks page in admin.

share|improve this answer

You would need to create a custom rewrite rule that maps not just the post slug but also the cid value, see this article:

http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html

And the relevant Codex documentation:

http://codex.wordpress.org/Rewrite_API http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

I'm assuming you don't want to use query strings, since in that case it would be as easy as using this URL:

example.com/custom-post-slug/custom-post-title/?cid=10

and getting the value this way:

$cid=isset($_GET['cid'])?$_GET['cid']:false;

However, keep in mind that the .../param_name/param_value/ syntax is not the best in terms of semantics and SEO. URLs are supposed to be hierarchical.

Some use /param_name:param_value/ instead, although the most "pure" solution would be to use ?param_name=param_value.

share|improve this answer

Try this in your init callback:

add_rewrite_rule(
    '^custom-post-type-slug/([^/]*)/cid/([^/]*)/?',
    'index.php?custom-post-type-slug=$matches[1]&cid=$matches[2]',
    'top'
);

add_rewrite_tag('%cid%', '([^&]+)');

You can then use the following to get your cid variable value:

global $wp_query;
$wp_query->query_vars['cid']

I used the following functions:

http://codex.wordpress.org/Rewrite_API/add_rewrite_rule http://codex.wordpress.org/Rewrite_API/add_rewrite_tag

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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