Do they also have a profile field for gender?
You could write custom mysql queries – more info.
Yes, they have.
The custom mysql queries look pretty interesting. Thank you.
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?
You’ll need global $wpdb;
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.
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;
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.
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.