Skip to:
Content
Pages
Categories
Search
Top
Bottom

xprofile_get_field_data not working at pre_get_posts


  • msolution
    Participant

    @msolution

    im working with pre_get_posts action hook, and calling xprofile_get_field_data in the function it is returning an empty string, after looking into the function it seems the $bp->profile array is not fully loaded, and $bp->profile->table_name_fields is missing and generating a mysql error.

    I have disabled all other plugins, and still the issue persists.

    what am i missing?

    WordPress 5.2.3
    BuddyPress Version 4.4.0
    this is an intranet site!
    Theme: Multi NewsVersion: 2.6.5 [www.momizat.com]

    The Issue does not happen on TwentyFifteen/ TwentyTwelve

    Looking for a fix.

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

  • shanebp
    Moderator

    @shanebp

    Due to something in your theme, you are calling xprofile_get_field_data too early.

    In your pre_get_posts filter, you could try calling BP: $bp = buddypress();

    Otherwise, you’ll need to talk to the theme creator.


    msolution
    Participant

    @msolution

    Thanks @shanebp ,

    will contact theme dev

    Thanks!


    Buddy Quaid
    Participant

    @buddyq

    Having the same type of issue. I cant use xprofile_get_field_data from the functions.php in a child theme.


    mashhood45
    Participant

    @mashhood45

    i’m also facing the same issue , i also check on tutorials on how to do it but in vein.agents of shield jacket


    msolution
    Participant

    @msolution

    For anyone facing similar issue, this is how i tackled it!

    $my_dept = xprofile_get_field_data( $field_id, get_current_user_id(), 'array' );
    if( empty( $my_dept ) ) {
     $my_dept = $wpdb->get_var( $wpdb->prepare( 'SELECT value FROM '.$wpdb->prefix.'bp_xprofile_data WHERE field_id = %d AND user_id IN ('.get_current_user_id().')', $field_id ) );
     }

    Hope this helps!

    M.


    shanebp
    Moderator

    @shanebp

    You should not need to write a custom query to retrieve a piece of profile data, but if you do then this is more distributive:

    global $wpdb;
    $bp = buddypress();
    $user_id = get_current_user_id();
    $field_id = 2; // for example
    $field_value = $wpdb->get_var( $wpdb->prepare( "SELECT value FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = $user_id", $field_id ) );
    var_dump( $field_value );

    msolution
    Participant

    @msolution

    i agree, this is better, thanks 🙂


    Buddy Quaid
    Participant

    @buddyq

    Hey there!!! Looks like using bp_ready worked! Thanks@


    shanebp
    Moderator

    @shanebp

    Using the proper hook always helps. Can be tricky to figure out.

    This is not up to date or comprehensive, but should help: bp hook order

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