Skip to:
Content
Pages
Categories
Search
Top
Bottom

Getting user_ids from database error


  • Leo
    Participant

    @wp_new_user

    Hello,

    I am trying to get user_ids of users that have field a certain filed in their profiles to show only them in a custom members loop.


    global $wpdb;
    $db_query = "SELECT user_id FROM wp_bp_xprofile_fields WHERE field_id = 2 AND value !empty";
    $match_ids = $wpdb->get_var($db_query);
    $get_these_members = '&per_page=20&type=newest&include=' .$match_ids;
    if ( bp_has_members($get_these_members) ) :

    I think there’s a syntax error, and !empty is not the correct way for this, I tried a lot of options non of the works.
    To test it, I tried this line:

    $db_query = "SELECT user_id FROM wp_bp_xprofile_fields WHERE field_id = 2 AND value = 45";

    where 45 is the value of the filed for one of the members, but it still doesn’t show any member.

    I found that I need to implode the user_ids list from the forum, but it doesn’t help neither.

    It returns ‘Sorry, no members were found.’
    when I enable wp_debug appears this error
    ‘WordPress database error: [Unknown column ‘user_id’ in ‘field list’]’

    Could you please help me?

    Thanks,
    Leo

Viewing 4 replies - 1 through 4 (of 4 total)

  • shanebp
    Moderator

    @shanebp

    ‘WordPress database error: [Unknown column ‘user_id’ in ‘field list’]’

    There is no user_id field in bp_xprofile_fields.
    The correct table is bp_xprofile_data.

    value is a text field, so you need quotes: AND value = '45'";

    Instead of AND value !empty"; try AND value != ''";

    $wpdb->get_var returns a single value. Try $wpdb->get_col.

    Your issues could have been solved by examining the table structure and reading reference docs like https://codex.wordpress.org/Class_Reference/wpdb.


    Leo
    Participant

    @wp_new_user

    Thank you @shanebp. there’s no error now and thanks for the reference, I’m going to need it.

    Here’s what works for me if someone needs it:

    global $wpdb;
    $db_query = "SELECT user_id FROM wp_bp_xprofile_data WHERE field_id = 2 AND value != ''";
    $match_ids = $wpdb->get_col($db_query);
    $members_imploded = '&per_page=20&type=newest&include=' . implode(",", $match_ids);
    if ( bp_has_members($members_imploded) ) :

    Regards,
    Leo


    shanebp
    Moderator

    @shanebp

    Glad you got it working.

    One issue is that you assume this prefix wp_.
    It may work for you, but to avoid that assumption do this:

    "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data …etc


    Leo
    Participant

    @wp_new_user

    Yes, correct, thanks @shanebp!

    Another thing, now my custom members loop is sorted – newest first.
    I just wonder if there’s such a thing ‘the filed last updated date‘ so that I could sort the loop by the filed update date?

    Thank you!
    Leo

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting user_ids from database error’ is closed to new replies.
Skip to toolbar