0

Hi I am struggling to figure out out how to make a filter for users posts by their profile fields.. example= Users registration form has select dropdown profile field of school years 2017, 2018 and then they create a post. filter all posts by that taxonomy of graduation year..so then you can only see posts by people of that school year. Is anyone aware of a plugin that could do this. the confusing part is posts are being loaded but filtered by member details. two separate directories.

Please help, or advice

0

First you can save in a associative array the value of the school field and the user id who belongs this field value. You need to know the school field ID.

define(FIELD_ID, /* Insert field id here */);    
/* Save all site users */
$users = bp_core_get_users();
/* Create array for save fields values */
$user_xprofile_school = array();

/* Loop over each user and save the id and the school field value */
foreach ( $users as $user ):
        $index = 0;
        foreach ( $user as $u ):
            /* Check if value exists before save */
            if ( xprofile_get_field_data( FIELD_ID, $u->id, 'array' ) ):
                $users_xprofile_school[$index]['user_id'] = $u->id;
                $users_xprofile_school[$index]['field_school'] = xprofile_get_field_data(FIELD_ID, $u->id, 'array');
                $index++;
            endif;
        endforeach;
    endforeach;

This is going to return an array like this:

array([0] => ([user_id] => 1, [field_school] => 'years 2017'), [1] => ([user_id] => 2, [field_school] => 'years 2018'))

Then you can loop the users posts using the field_school index and the user_id.

This is not all the work but could be a good start point. Hope this help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.