0

I'm trying to add a query parameter to a custom template in order to look up a record from the DB with the ID passed in. Here's how I'm attempting to do it:

At the very top of my custom template, I add the college_id param to the query_vars filter.

function custom_query_vars_filter($vars) {
  $vars[] = 'college_id';
  return $vars;
}

add_filter( 'query_vars', 'custom_query_vars_filter' );

Then, in my content lower down, I attempt to get that query variable and output it to the screen, as well as look up the record in the database. Here's how I'm doing that:

$college_to_delete = $wp_query->query_vars['college_id'];
echo $college_to_delete;
echo 'query'.get_query_var('college_id');
global $wpdb;
$colleges = $wpdb->get_results('SELECT * from '.$college_table.' WHERE ID='.$college_id.';');

The idea is that I'll grab the college_id query variable, display it to the screen, and then SELECT the record from the database whose ID matches the query ID. What am I doing wrong? Totally new wordpress developer so I'm sure I'm missing something simple.

  • How are you setting college_id? Requests are parsed long before the template is loaded, your filter should be in a plugin file or theme's functions.php. – Milo Jun 1 '17 at 18:03
  • Oh, interesting. I can definitely try that. I'm setting college_id through window.location in javascript, as I want to use a confirm() to make sure the user wants to delete that college. – Alex Kibler Jun 1 '17 at 18:04
  • Aha! Declaring the custom query_vars in functions.php did the trick! – Alex Kibler Jun 1 '17 at 18:06
  • please add it as an answer and accept it – Mark Kaplun Jun 1 '17 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.