Skip to main content
Added additional information
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

TheThere's the WordPress meta API for retrieving meta values from the wp_postmeta table, but the API doesn't provide a function for retrieving the post ID (post_id value), but you can use custom SQL as in your code.

However, $wpdb->get_col() returns an array of values (upon success), so that's why you get the "Array" when you do echo $get_values;.

So you can either do something like this:

$post_ids = $wpdb->get_col( $prepare_query$prepare_guery ); // returns an array of IDs on success
$post_id = $post_ids[0]; // simplified - assuming that $post_ids[0] is actually set
var_dump( $post_ids ); // for testing

Or (a simpler option) if you want to retrieve just a single value, you should use $wpdb->get_var():

$post_id = $wpdb->get_var( $prepare_query$prepare_guery ); // returns a string (an ID) on success

Additional Notes

$prepare_guery = $wpdb->prepare( "SELECT post_id FROM $tbl WHERE meta_key ='patientid' AND meta_value LIKE $thevalue", $meta_value );

should be:

$prepare_guery = $wpdb->prepare( "SELECT post_id FROM $tbl WHERE meta_key ='patientid' AND meta_value LIKE %s", $wpdb->esc_like( $thevalue ) );

I.e. Note the %s (placeholder) and I also use $wpdb->esc_like(). I also realized that the $meta_value is not defined in your code, hence I changed that (in the above code) to $thevalue.

And in the previous revisions, I misspelled the $prepare_guery as $prepare_query... or did you mean $prepare_query ?

The WordPress meta API doesn't provide a function for retrieving the post ID (post_id value), but you can use custom SQL as in your code.

However, $wpdb->get_col() returns an array of values (upon success), so that's why you get the "Array" when you do echo $get_values;.

So you can either do something like this:

$post_ids = $wpdb->get_col( $prepare_query ); // returns an array of IDs on success
$post_id = $post_ids[0]; // simplified - assuming that $post_ids[0] is actually set
var_dump( $post_ids ); // for testing

Or (a simpler option) if you want to retrieve just a single value, you should use $wpdb->get_var():

$post_id = $wpdb->get_var( $prepare_query ); // returns a string (an ID) on success

There's the WordPress meta API for retrieving meta values from the wp_postmeta table, but the API doesn't provide a function for retrieving the post ID (post_id value), but you can use custom SQL as in your code.

However, $wpdb->get_col() returns an array of values (upon success), so that's why you get the "Array" when you do echo $get_values;.

So you can either do something like this:

$post_ids = $wpdb->get_col( $prepare_guery ); // returns an array of IDs on success
$post_id = $post_ids[0]; // simplified - assuming that $post_ids[0] is actually set
var_dump( $post_ids ); // for testing

Or (a simpler option) if you want to retrieve just a single value, you should use $wpdb->get_var():

$post_id = $wpdb->get_var( $prepare_guery ); // returns a string (an ID) on success

Additional Notes

$prepare_guery = $wpdb->prepare( "SELECT post_id FROM $tbl WHERE meta_key ='patientid' AND meta_value LIKE $thevalue", $meta_value );

should be:

$prepare_guery = $wpdb->prepare( "SELECT post_id FROM $tbl WHERE meta_key ='patientid' AND meta_value LIKE %s", $wpdb->esc_like( $thevalue ) );

I.e. Note the %s (placeholder) and I also use $wpdb->esc_like(). I also realized that the $meta_value is not defined in your code, hence I changed that (in the above code) to $thevalue.

And in the previous revisions, I misspelled the $prepare_guery as $prepare_query... or did you mean $prepare_query ?

Improved wording
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

IfThe WordPress meta API doesn't provide a function for retrieving the post ID (post_id value), but you can use custom SQL as in your code.

However, $wpdb->get_col() returns an array of values (upon success), so that's why you get the "Array" when you do echo $get_values;.

So you can either do something like this:

$post_ids = $wpdb->get_col( $prepare_query ); // returns an array of IDs on success
$post_id = $post_ids[0]; // simplified - assuming that $post_ids[0] is actually set
var_dump( $post_ids ); // for testing

Or (a simpler option) if you want to retrieve just a single value, you should use $wpdb->get_var():

$post_id = $wpdb->get_var( $prepare_query ); // returns a string (an ID) on success

But you can also use $wpdb->get_col(). Just remember — on success — it returns an array and not a string:

$post_ids = $wpdb->get_col( $prepare_query );
$post_id = $post_ids[0]; // simplified and assuming this is set

If you want to retrieve just a single value, you should use $wpdb->get_var():

$post_id = $wpdb->get_var( $prepare_query );

But you can also use $wpdb->get_col(). Just remember — on success — it returns an array and not a string:

$post_ids = $wpdb->get_col( $prepare_query );
$post_id = $post_ids[0]; // simplified and assuming this is set

The WordPress meta API doesn't provide a function for retrieving the post ID (post_id value), but you can use custom SQL as in your code.

However, $wpdb->get_col() returns an array of values (upon success), so that's why you get the "Array" when you do echo $get_values;.

So you can either do something like this:

$post_ids = $wpdb->get_col( $prepare_query ); // returns an array of IDs on success
$post_id = $post_ids[0]; // simplified - assuming that $post_ids[0] is actually set
var_dump( $post_ids ); // for testing

Or (a simpler option) if you want to retrieve just a single value, you should use $wpdb->get_var():

$post_id = $wpdb->get_var( $prepare_query ); // returns a string (an ID) on success
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

If you want to retrieve just a single value, you should use $wpdb->get_var():

$post_id = $wpdb->get_var( $prepare_query );

But you can also use $wpdb->get_col(). Just remember — on success — it returns an array and not a string:

$post_ids = $wpdb->get_col( $prepare_query );
$post_id = $post_ids[0]; // simplified and assuming this is set