0

I have a ref query string which matches rows in my wp_invites table.

In my wp_invites table, I have the following columns:

  • lead_name
  • reference

Let's assume here is one of the rows:

  • Lead name: Freddy
  • Reference: 4FxtfVFszCHd

When the user accesses their invite, their URL will be something like: test.test/?ref=4FxtfVFszCHd

I'm using query_vars to handle the URL parameters. Then accessing the row in the DB where the query string matches reference in the table column.

Here is my current approach:

<?php

/*
* Step 1:
* Get reference query param from URL
*/

function add_query_vars_filter( $vars ){
  $vars[] = "ref";
  return $vars;
}

add_filter( 'query_vars', 'add_query_vars_filter' );



/*
* Step 2:
* Get data from row in wp_invites table where reference equals what's in the query string
*/

$reference = sanitize_text_field( get_query_var('ref') );

global $wpdb;

$table_name = $wpdb->prefix . 'invites';

$results = $wpdb->get_results(
  $wpdb->prepare( "SELECT * FROM $table_name WHERE reference=%s", $reference), ARRAY_A
);


var_dump($results);
  
if( $results ){
  foreach ($results as $result){
    echo '<p>'. $result['lead_name'] . '</p>';
  }
}

?>

Here are my results:

  • The above just prints Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
  • My lead_name echo doesn't print
  • A var_dump($vars) and ref returned empty
  • A var_dump($results) returns array(0){}

I think my issues are stemming because return $vars; isn't returning a value for ref?

Edit:

My approach follows what's listed in the official docs.

Doing the following to access the var returns NULL.

global $wp_query;
var_dump($wp_query->vars);

Unsure how to proceed. My URL definitely has a ref query string

1
  • Where is this code? You shouldn't really be outputting anything directly inside functions.php or a plugin. The output should be done inside a hook, block or shortcode or something like that. Your code is probably running before the query vars have been parsed. Regardless, in your case ref is not being used to query posts, so it shouldn't be a query var. Just use $_GET['ref'] to get the value. Dec 4, 2022 at 15:24

1 Answer 1

0

I think the issue may be that you think query_vars can pick up the query string arguments from a URL, but you don't need to do it that way. That's primarily used if you're needing to get this into the WP Query (which isn't what you're doing here). It's much easier to simply check for the query param with $_GET.

Here's how you need to approach it:

<?php

/*
* Step 1:
* Get reference query param from URL
*/

// Check with isset() so you don't get undefined errors.
// Use a null value as a default so your query will still run.
$reference = ( isset( $_GET['ref'] ) ) ? sanitize_text_field( $_GET['ref'] ) : '';

/*
* Step 2:
* Get data from row in wp_invites table where reference equals what's in the query string
*/

global $wpdb;

$table_name = $wpdb->prefix . 'invites';

$results = $wpdb->get_results(
  $wpdb->prepare( "SELECT * FROM $table_name WHERE reference=%s", $reference), ARRAY_A
);


var_dump($results);
  
if( $results ){
  foreach ($results as $result){
    echo '<p>'. $result['lead_name'] . '</p>';
  }
}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.