Query is returning an empty array (Mysql/Wordpress)

Asked

Viewed 150 times

0

How do I get the field values meta_value = candidate of this table.

inserir a descrição da imagem aqui

I’m using this query, but it’s returning an empty array.

$resultados2 = $wpdb->get_results( "SELECT * FROM $wpdb->usermeta WHERE $wpdb->usermeta.meta_key = 'wp_capabilities' AND $wpdb->usermeta.meta_value = 'candidate';");

print_r($resultados2);

I want to reclaim the fields candidate.

1 answer

3


LIKE OPERATOR

The array is being returned empty because you are looking for 'candidate' (exactly) and actually the record comes as "a:1:{s:9:"candidate";b:1;}". LIKE will perform a rough search and return records that contain "candidate".

Simply change your query as follows:

$wpdb->usermeta.meta_value LIKE '%candidate%'

The % are wildcards (jokers) that allow any value before or after the term sought to be accepted. You could use % only before and only after the value, but in your case it is necessary to use both.

  • 2

    Rounding out the answer... The array is being returned empty because you are looking for 'candidate' (exactly) and actually the record comes as "a:1:{s:9:"candidate";b:1;}". LIKE will perform a rough search and return records that contain "candidate".

  • @Jefersonleonardo I took the liberty of increasing the response with your suggestion, okay?

  • Okay, @Thiago Santos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.