I’m looking to pass a variable in a URL as www.example.com/view-job?id=155
, however WordPress doesn’t allow this to be done as you’d normally do in PHP.
I have found a function, which is widely used:
function add_custom_query_var($vars){
$vars[] = "ID";
return $vars;
}
add_filter( 'query_vars', 'add_custom_query_var' );
And I’m supposed to use it to a link as another page as such:
<?php add_query_arg( 'ID', $job_opening_id, site_url('/view-job/'))?>
The tricky part though, is that $job_opening_id
is set in a for loop, and it seems that the action method doesn’t take add_query_arg
as an argument. Here’s how I do it:
<? for($i = 0; $i < count($rowArray); $i++) { ?>
<form method="post" action="<?php add_query_arg('ID', $job_opening_id, site_url('/job-openings/view-job/'))?>">
<? $job_opening_id = $data->response->result->JobOpenings->row[$i]->FL[0]->content;
}
<td class="jobopenings-td">
<input type="hidden" name="job_id" value="<?php echo $job_opening_id ?>" />
<button id="jobopening-link">
<? echo $data->response->result->JobOpenings->row[$i]->FL[1]->content; ?>
</button>
</td>
</form>
I’m not quite sure how to go about this. I’m convinced it’s supposed to be a parameter in the action method, as it’s supposed to be redirecting to another page with the variable in its URL.
1.First of all you have to echo the url in the value of action attribute.
2. Why you are only looping start section of a form but not its end. You supposed to loop the whole form.
3. Currently you are using $job_opening_id before assigning a value to it. You must first assign the value and then use it.