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.

As we know WordPress supports multiple tag queries (',' and '+'). The only problem with this add_query_arg() doesn't handle these methods if a query param is already exists in the URL.

From the following url www.domain.com?post_type=ptype&param=value1 add_query_arg('param', 'value2') will generate www.domain.com?post_type=ptype&param=value2 insead of www.domain.com?post_type=ptype&param=value1,value2 or value1+value2

I would only use comma separated (OR) method. Could you please help me to write a function which adds additional param value next to an exsisting one? regular expressions perhaps?

share|improve this question

1 Answer 1

up vote 0 down vote accepted
$url = parse_url( $your_url );
$query = $url['query'];

$args_arr = array();
parse_str( $query, $args_arr );

if( isset( $args_arr['param'] ) ) {
    $query_string = $args_arr['param'];
    $query_string .= ',value2';
} else {
    $query_string = 'value2';
}

add_query_arg( 'param', $query_string );

That's completely untested, but you get the concept. Basically, wordpress is gonna want to replace what you have set in param, so you need to get what you have in param, then add what you want to add to it. You can definitely make that into a function if you want to, depends on your usage case.

share|improve this answer
    
thanks it is a good idea, works great! –  user9909 Mar 22 '12 at 16:44

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.