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?

$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.

|improve this answer|||||

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.