Skip to:

Problem with SQL query for a custom member loop

  • enaeld


    Hello everyone, I hope you are well.
    I come to you because I have a problem with a sql query.

    To put you in context, I’m trying to make a custom members loop based on the values ​​of the extended profile fields of the members.

    For that I used the codex and in this one they show how to create a function “my_custom_ids” which allows to recover the ids of all the users who put the same value in ONE field.

    I decided to modify this function to be able to retrieve the ids of the users having SEVERAL values ​​of fields in common.

    Here is the SQL query present in the basic function “my_custom_ids” :
    $query = “SELECT user_id FROM ” . $wpdb->prefix . “bp_xprofile_data WHERE field_id = ” . $field_id;
    $query .= ” AND value = ‘” . $field_value . “‘”;

    Here is my SQL query in my custom “my_custom_ids” function :
    $query = ‘SELECT user_id FROM ‘ . $wpdb->prefix . ‘bp_xprofile_data WHERE (field_id = ‘ . $users_gender_form_id. ‘ AND value = “Female”) AND (field_id = ‘ . $users_hair_color_form_id. ‘ AND value = “Blonde”)’;

    I do not know if it’s clear but I’m trying to retrieve the ids of users who select the “female” value in the extended profile field “gender” and the value “blonde” in the extended prodile field “hair color “.

    But of course it does not work ! haha

    When I separate my sql query in two:
    $query = ‘SELECT user_id FROM ‘ . $wpdb->prefix . ‘bp_xprofile_data WHERE (field_id = ‘ . $users_gender_form_id. ‘ AND value = “Female”)’;

    or :

    $query = ‘SELECT user_id FROM ‘ . $wpdb->prefix . ‘bp_xprofile_data WHERE (field_id = ‘ . $users_hair_color_form_id. ‘ AND value = “Blonde”)’;

    it works. I do not know why when I combine the two it does not work… If you have any leads to give me I would be greatly grateful !

    Thank you very much, have a good day !

    (I use WordPress 5.2.2 and Buddypress 4.4.0)

  • You must be logged in to reply to this topic.
Skip to toolbar