Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display filtered overview of members

  • @barodscheff

    Participant

    Hi!

    Unfortunately I need your help once again.

    Every member on my page has got a profile field called ‘Sport’. It contains a select list with different sports.
    Now I would like to display a summerized overview of all members on my home page. They should be distinguished by gender and sport. It should look like this:

    Men:
    Soccer (12 members)
    Baseball (100 members)
    Football (5 members)

    Women:
    Soccer (2 members)
    Handball (50 members)

    How can I do this?
    I’m able to work with PHP but I have no idea where to start.
    Any suggestions?

    Thank you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • @shanebp

    Moderator

    Do they also have a profile field for gender?

    You could write custom mysql queries – more info.

    @barodscheff

    Participant

    Yes, they have.

    The custom mysql queries look pretty interesting. Thank you.

    @barodscheff

    Participant

    Ok, I’ve tried the following to display the amount of male and female members first:

    // Get all male members from DB
    $male = $wpdb->get_var( "SELECT COUNT(id) FROM $wpdb->ar_bp_xprofile_data WHERE field_id = 11 AND value LIKE 'male'" );
    // Get all female members from DB
    $female = $wpdb->get_var( "SELECT COUNT(id) FROM $wpdb->ar_bp_xprofile_data WHERE field_id = 11 AND value LIKE 'female'" );
    
    // Print amount of male and female members
    echo $male;
    echo $female;
    

    But it’s not working. Any ideas?

    @henrywright

    Moderator

    You’ll need global $wpdb;

    @shanebp

    Moderator

    Please use the ‘code’ button to format your code.

    Is ‘ar’ your $table_prefix, as defined in wp-config.php ?
    If so, then this:
    $wpdb->ar_bp_xprofile_data
    Should be this:
    $wpdb->bp_xprofile_data

    also – Why use ‘LIKE’ if you’re looking for exact matches?
    value = 'female' is faster.

    @barodscheff

    Participant

    Thank you for your answers.

    I’ve tried the example from the wordpress codex:
    $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );

    It works even when I do not use global $wpdb;.
    First question: Why does it work without global $wpdb?

    Second question: Why do I have to use $wpdb->bp_xprofile_data instead of $wpdb->ar_bp_xprofile_data? Does the prefix do not belong to the name? When I try the query in phpmyadmin I also have to use the prefix.

    Unfortunately my own queries still not work. I simplified my query now:

    global $wpdb;
    $male = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->bp_xprofile_data");
    echo $male;

    @henrywright

    Moderator

    Regarding the $wpdb object, take a look here for more info:

    https://codex.wordpress.org/Class_Reference/wpdb#Using_the_.24wpdb_Object

    @barodscheff

    Participant

    Ok, I solved it.
    For those who are interested in the solution: you have to use the prefix

    // Set $wpdb global =  WPrequirement
    global $wpdb;
    
    // Get all male members from DB
    $male = $wpdb->get_var( "SELECT COUNT(user_id) FROM ar_bp_xprofile_data WHERE value = 'male' AND user_id IN (SELECT user_id FROM ar_bp_xprofile_data WHERE value = 'Soccer')" );
    echo 'Male: ' . $male . '<br>';
    
    // Get all female members from DB
    $female = $wpdb->get_var( "SELECT COUNT(user_id) FROM ar_bp_xprofile_data WHERE value = 'female' AND user_id IN (SELECT user_id FROM ar_bp_xprofile_data WHERE value = 'Soccer')" );
    echo 'Female: ' . $female . '<br>';

    Thank you for your help.

    @barodscheff

    Participant

    I just forgot one question.
    Is there a way to call this query in the backend editor?
    I’m using the visual composer and would like to call the query / php template in the editor.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Display filtered overview of members’ is closed to new replies.
Skip to toolbar