Skip to:
Content
Pages
Categories
Search
Top
Bottom

‘Date of birth’ field not being returned because private. Workaround?


  • panosa1973
    Participant

    @panosa1973

    This is happening in functions.php to fire up in members_loop:
    Even though the Date of Birth is a private field I still need to pull it so I can calculate and display the user’s age. The below code works fine when I’m logged in as Admin but returns 0 when I’m logged in as a regular user which made me realize it’s probably because it’s set to private.
    $dod = bp_get_member_profile_data( 'field=Date of birth' )

    How do I get the field value straight from wp_bp_xprofile_fields?
    The below isn’t working. I’m assuming it’s terribly wrong. I think I need to include in the db query the userID because it only pulls one value no matter who the user is but how do I get the displayed user’s ID (if that’s the case to begin with)? I managed to get this to work when viewing one’s profile page but I can’t seem to do it in members_loop.

    //Add fields in the member directory results
    function add_info_to_members_loop() {
    //$dateOfBirth = bp_get_member_profile_data( 'field=Date of birth' );//This isn't working
    
    global $wpdb; 
    $dateOfBirth = array();
    $dateOfBirth = $wpdb->get_col("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2");
    $dob = $dateOfBirth[0];
    
    /*This will work when I finally get the date of birth
    $today = date("Y-m-d");
    $diff = date_diff(date_create($dob), date_create($today));
    $state = bp_get_member_profile_data( 'field=State' );
    echo 'Age '.$diff->format('%y')  . ' - In ' . $state;
    */
    }
    add_action( 'bp_directory_members_item', 'add_info_to_members_loop' );

    Thank in advance.

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

  • Venutius
    Moderator

    @venutius

    By the looks of things you just need to change this:

    
    global $wpdb;
    $user_id = bp_displayed_user_id(); 
    $dateOfBirth = array();
    $dateOfBirth = $wpdb->get_col("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND user_id = $user_id");
    $dob = $dateOfBirth[0];
    
    /*This will work when I finally get the date of birth
    $today = date("Y-m-d");
    $diff = date_diff(date_create($dob), date_create($today));
    $state = bp_get_member_profile_data( 'field=State' );
    echo 'Age '.$diff->format('%y')  . ' - In ' . $state;
    */
    }
    add_action( 'bp_directory_members_item', 'add_info_to_members_loop' );

    Think that should do it.


    panosa1973
    Participant

    @panosa1973

    It looked like it would do it but bp_displayed_user_id(); returns 0. There must be a way to get the ID of the user who’s inside the current loop.


    Venutius
    Moderator

    @venutius

    Did you try calling:

    $bp = buddypress();
    $user_id = $bp->displayed_user->id;

    Venutius
    Moderator

    @venutius

    Ah sorry, in the loop you would use bp_member_user_id()


    panosa1973
    Participant

    @panosa1973

    Ok, tried that too. This one doesn’t return anything either.


    Venutius
    Moderator

    @venutius

    one I’ve used in similar circumstances is bp_get_member_user_id()


    panosa1973
    Participant

    @panosa1973

    Yes! bp_get_member_user_id() did the job.
    My working code now is:

    //Add fields in the member directory results
    function add_info_to_members_loop() {
    	global $wpdb; 
    	$dateOfBirth = array();
    	$user_id = bp_get_member_user_id();
    	$dateOfBirth = $wpdb->get_col("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND user_id = $user_id");
    	$dob =  $dateOfBirth[0];
    
    $today = date("Y-m-d");
    $diff = date_diff(date_create($dob), date_create($today));
    $state = bp_get_member_profile_data( 'field=State' );
    echo 'Age '.$diff->format('%y')  . ' - In ' . $state;
    //This will output i.e.: Age 35 - In New York 
    }
    add_action( 'bp_directory_members_item', 'add_info_to_members_loop' );

    Thank you @venutius!


    Venutius
    Moderator

    @venutius

    That’s great! Coding brings these regular victories, it’s why it can be so addictive. That and the constant challenge.


    panosa1973
    Participant

    @panosa1973

    Very true 🙂
    Thanks again.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Skip to toolbar