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.

This problem concerns several rewrite rules, but i'll stick to one, for clarity. In my theme, i use a parameter "parentid" which gives me the page/post/custom post i've clicked on (the parent).

in content.php instead of

<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>

i put

<a href="<?php the_permalink(); ?>&parentid=30" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>

In this example i arbitrarily set parentid=30, in my code,i would have a value retrieved by get_query_var('p') for instance.

in functions.php

//I declare the custom query var
function add_query_vars($query_vars)
{
    $query_vars[] = "parentid"; 
    return $query_vars;
}
add_filter('query_vars', 'add_query_vars');

and in header.php

$parentid = get_query_var( 'parentid');

So in my case, i have links like

?p=12&parentid=30

using permaling rewriting, how to add the custom parameter "parentid" ? I tried

function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['([^/]+)/([^/]+)$'] = '?p=$matches[1]&parentid=$matches[2]';
    return $newrules + $rules;
}
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');

but "obviously" it doesn't work. With that rule, the url becomes

/wordpress/nom-de-l-article/&parentid=30/

instead of

/wordpress/nom-de-l-article/30/

In fact, in this case, the generic rewrite rule is applied

my rule is ignored because using the plugin Monkeyman Rewrite Analyzer, my rule, first in the list, has the parameter "p" crossed with the comment : this query variable is not public and will not be saved

My questions :

  • what rule should i write, to take into account this custom parameter for a "standard" post (?p=xx&parentid=yy)
  • Why parentid is not retrieved when the rewrite is active ? if i echo get_query_var( 'parentid'); without rewrite i get "30" but with rewrite, i get "&parentid=30" !
share|improve this question
    
you get back &parentid=30, because that's what you're inserting at the end of your URL. if you want just 30, then your URL should be href="<?php the_permalink(); ?>30/". also, p is supposed to be a post id, if you're passing the slug, you want to use name=. –  Milo Apr 11 '13 at 14:41
    
I'm not sure to understand your answer. for a standard post href="<?php the_permalink(); ?>" gives ?p=whateverid without rewrite but /nameofpost/ when permalink are activated. So if i set href="<?php the_permalink(); ?>&parentid=30" i retrieve 30 when i echo get_query_var( 'parentid') which is what expected, but this echo gives &parentid=30 when permlink are rewritten. If i do href="<?php the_permalink(); ?>30, the permalink will lead to 404 because if will be like ?p=whateverid30 how would you get the extra parameter? –  user29296 Apr 11 '13 at 15:25

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.