Sunday, December 4, 2022
HomeWordPress Developmentsql - query_vars would not return question string (attempting to get knowledge...

sql – query_vars would not return question string (attempting to get knowledge from $wpdb)


I’ve a ref question string which matches rows in my wp_invites desk.

In my wp_invites desk, I’ve the next columns:

Let’s assume right here is among the rows:

  • Lead title: Freddy
  • Reference: 4FxtfVFszCHd

When the person accesses their invite, their URL will likely be one thing like: check.check/?ref=4FxtfVFszCHd

I am utilizing query_vars to deal with the URL parameters. Then accessing the row within the DB the place the question string matches reference within the desk column.

Right here is my present strategy:

<?php

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

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

add_filter( 'query_vars', 'add_query_vars_filter' );



/*
* Step 2:
* Get knowledge from row in wp_invites desk the place reference equals what's within the question string
*/

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

world $wpdb;

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

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


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

?>

Listed below are my outcomes:

  • The above simply prints Welcome to WordPress. That is your first submit. Edit or delete it, then begin writing!
  • My lead_name echo would not print
  • A var_dump($vars) and ref returned empty
  • A var_dump($outcomes) returns array(0){}

I believe my points are stemming as a result of return $vars; is not returning a worth for ref?

Edit:

My strategy follows what’s listed within the official docs.

Doing the next to entry the var returns NULL.

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

Not sure easy methods to proceed. My URL undoubtedly has a ref question string

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments