1

I have a function in a WordPress plugin I am about to write (my first one) that should check if a given ID belongs to a user with a certain role ('dance_couple' in this case).

$sc_user_id is the id passed in the shortened code.

function user_is_dance_couple($sc_user_id){
   global $wpdb;
   $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->usermeta WHERE user_id = %d AND meta_value LIKE `%dance_couple%`", $sc_user_id));
   if($count == 1){ return true; } else { return false; }
}

if (user_is_dance_couple(1)) {
  // here comes what should be done if the user has the right role
}

Question: The weird thing is, that I copied a simpler version of the function (https://wordpress.stackexchange.com/questions/165691/how-to-check-if-a-user-exists-by-a-given-id) and adapted it. so the only thing I changed is the bit of sql code (that works fine in sql), the function as it was before also works fine - I have no idea what I did wrong.

I already tried:

  1. changing the single quotes to the thing between the double quotes " ` "
  2. taking the sting out and adding it in a variable
  3. adding the table names in before the columns

Thanks for the help!

4
  • $string is not doing anything, so it should be removed Commented Jan 6, 2017 at 3:42
  • @maya Would you please remove $string variable from your query and after check it? Commented Jan 6, 2017 at 4:36
  • %dance_couple% should be '%dance_couple%' Commented Jan 6, 2017 at 5:30
  • $string variable is gone - but can't be the problem ... wasn't there before (as explained in list, that just was one of several tries) @itzmukeshy7 - I tried that - didn't work :( Commented Jan 6, 2017 at 12:50

2 Answers 2

1

No need for SQL queries. You could do something like that:

$user_id = 1; # or whatever user you need
$role_to_find = 'dance_couple';

$user = get_userdata( $user_id );

if( false !== $user ){
    # User found, do your stuff
    $user_roles = $user->roles;
    # var_dump( $user_roles );

    if( in_array( $role_to_find, $user_roles ) ){
        echo 'User has the role';
    }else {
        echo 'User does not have the role';
    }
}else{
    echo 'User not found';
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot - I already had something similar - but without the second variable in "in_array" - maybe the plain text killed it or I made some other mistake.
0

I don't think you can use the GLOBAL object $wpdb inside a query and inside the quotation. What I would do is create another variable and put the value of $wpdb->usermeta in that variable.

in this way:

$user = $wpdb->usermeta;
global $wpdb;
$query = "SELECT COUNT(*) FROM %d WHERE user_id = %d AND meta_value LIKE `%dance_couple%`";
$count = $wpdb->get_var($wpdb->prepare($query, $user, $sc_user_id));

Or the long version:

$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM %d WHERE user_id = %d AND meta_value LIKE `%dance_couple%`", $user, $sc_user_id));

The syntax of the query looks correct, try in this way

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.