homepage Welcome to WebmasterWorld Guest from 54.146.190.193
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / WordPress
Forum Library, Charter, Moderators: lorax & rogerd

WordPress Forum

    
Find a specific user based on custom fields
surrealillusions

5+ Year Member



 
Msg#: 4441995 posted 2:42 pm on Apr 17, 2012 (gmt 0)

Hi all,

I would like to display a certain user's details based on a custom field.

I've had a look at around Google, the get_user() function, and various scripts that display lists of users...but can't get them to work how I'd like them.

The custom fields I need to check are within the wp_usermeta database table.

I have a site where there are areas, and each area has a group leader. The 2 custom fields are where its determined that this user is a designated group leader. Of course, there are several groups, and I'm using one page template to cover all areas, with the right information displayed depending on the area/page.

Here's what I've tried to use, but it displays all users:

I've tried to adjust the select statement to select just the custom fields, but to no avail. It either displays all users or none at all.


$szSort = "user_nicename";

// Custom query

$aUser_ids = $wpdb->get_col("SELECT * FROM $wpdb->users ORDER BY $szOrder");

// Once we have the IDs we loop through them with a Foreach statement.
foreach ( $aUsersID as $iUserID ) :

// We use get_userdata() function with each ID.

$user = get_userdata( $iUserID );

// Here we finally print the details wanted.

echo $iUserID;
echo '<li>' . ucwords( strtolower( $user->first_name . ' ' . $user->last_name ) ) . '</li>';

endforeach; // end the users loop.


Any help would be appreciated!

 

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4441995 posted 4:02 pm on Apr 17, 2012 (gmt 0)

Well, I'm 99% sure you don't need to select directly from the database. It's drawing all user data because you're selecting all records without a where clause to limit the results. Then you loop through the entire database to "find what you want." An aside, this is EXTREMELY intensive as those arrays have to be held in memory. Wordpress is often slow enough on it's own, don't make it worse with inefficient programming. :-)

In any case, you can bet there's already a function for this, we just need to find out which one applies.

So I'm gathering that you either
- Want to display, to a group leader, all of the members in his group, or
- you want to display the group leader to group members, or
- some combination of both.

What you haven't told us is what is the nature of the custom field, and how does it relate to it's task? What key and value does it hold? The closest we get is

The 2 custom fields are where its determined that this user is a designated group leader.


So, okay, this user's a group leader, how does that relate to the task? It's easy enough to get their info [codex.wordpress.org] and display it to THEM, but in itself that's not all that helpful.

surrealillusions

5+ Year Member



 
Msg#: 4441995 posted 4:43 pm on Apr 17, 2012 (gmt 0)

Thanks for the quick reply.

I realise that using the select directly from the database is probably not a good idea, it was part of what seemed like a relatively straight forward solution that i found online...which didn't work :)


- Want to display, to a group leader, all of the members in his group, or
- you want to display the group leader to group members


Eventually, I'd like to do both. Although I originially asked to find out the first, but a solution to both would be great :)

The 2 custom fields.
The one field is the name of the area, which isn't unique, and lots of other people will have that. Its pre-populated from a drop down box, so whatever the area name is, its going to be known. For example "Scotland" out of a choice of England, Scotland, Wales and Ireland.

The areas are actually smaller in the project, but those are more well known ;)

For the user to be a leader, an admin ticks a box in the admin area and the value when that box ticked is "yes_area_leader". There is no value when it isn't ticked.

Some area's wont have a leader to start off with. Users need to apply for it via an email form. So, the users ID isn't going to be known beforehand.

Hope that helps!

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4441995 posted 4:08 pm on Apr 19, 2012 (gmt 0)

OK, well, it's still a little vague as the conditions under which you display user info, and how which users - but I'd go something like this path. Not working code, look up the functions in wordpress. You'll probably most likely experiment with list authors [codex.wordpress.org] or WP_User_query [codex.wordpress.org].

Something like

$args = Array(
'meta_value' => 'Scotland'
);

That's not going to be correct, and you likely don't want to hard code it. PROBABLY you'd get the current user, find out what their area is, then store it n a variable like

$args = Array(
'meta_value' => $this_user_area
);

That **should** work, because per the documentation, you can query by meta value, and your meta value 'Scotland' is assigned to this user. Note it's not querying by the meta key ("area") it's just looking for the value (I think.) So this should look for all users with this user's area.

You can put args right in the function call, but an array is likely best because you may want to add other args (like order by, etc.)


$wp_user_search = new WP_User_Query($args);
$other_members = $wp_user_search->get_results();
if (!empty($other_members)){
echo '<ul>';
foreach ($other_members as $member) {
$info = get_userdata($member->ID);
// If you don't want "this" member's name in the list . . .
// To display all including this user, remove if block
if ($member->ID != $this_user_id) {
echo '<li>'.$info->first_name.' '.$info->last_name.'</li>';
}
}
echo '</ul>';
}
else { echo '<p>There are no other members in this group.</p>'; }


That's not working code, but now that I look at it - there's no reason I can see it wouldn't work. :-) This is why I do this stuff - you just forced me to learn something new.

surrealillusions

5+ Year Member



 
Msg#: 4441995 posted 9:25 pm on Apr 19, 2012 (gmt 0)

Thanks :) Works a treat for displaying the users. I'm sure I can work out how to display them just for the area leader or admins only, by using the current user data functions.

Now..to display just the area leader to users.
Could you just add it to the $args array? Hmm..maybe not actually..as then it wouldn't display the users.

I can't just duplicate that and change the meta value, as then it would display all the leaders.

I need an if area_leader = true AND area = Scotland, then display user data.

What about changing the if statement?
if ($member->ID != $this_user_id)

To find the area_leader value?

if ($member->area_leader == "yes_area_leader") {

Doesn't work...doesn't even echo the else statement.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4441995 posted 3:21 pm on Apr 20, 2012 (gmt 0)

Hah! It worked, sweet. :-P

Think it through, you almost got it. :-)

What is it that gets the meta value?

Right . . . and since you're looking for the meta_value of 'Scotland', it makes sense there's no meta value for area leader (and if there was, it would be '1'). I think you may need to do a nested query. Since the area leader value is more or less boolean (if it's there, it's a leader, otherwise it's absent) all you'll need to do is check for the existence of the key for the current user in your loop. That's why it will have to be nested.


$wp_user_search = new WP_User_Query($args);
$other_members = $wp_user_search->get_results();
if (!empty($other_members)){
echo '<ul>';
foreach ($other_members as $member) {
$info = get_userdata($member->ID);
$is_leader = $leader_check = null; // reset for each iteration
// taking out the "if"
echo '<li>';
$leader_check = new WP_User_Query(array('meta_key'=>'area_leader')); // see below
$is_leader = $leader_check->get_results();
if (!empty($is_leader)) { echo '<strong>Area Leader:</strong> '; }
echo $info->first_name.' '.$info->last_name.'</li>';
}
echo '</ul>';
}
else { echo '<p>There are no other members in this group.</p>'; }


As is that won't work - it will return a list of ALL users assigned as area leaders, and you want a query that will restrict it to the user in your loop. This MIGHT work but it's a guess - ID = user id.

$leader_check = new WP_User_Query(array('ID'=>$info->ID','meta_key'=>'area_leader'));

surrealillusions

5+ Year Member



 
Msg#: 4441995 posted 4:38 pm on Apr 23, 2012 (gmt 0)

Thanks so far.

However, a stumbling block.

The 'meta_key'=>'area_leader' returns them all as area leaders, which isn't right. Changing it to 'meta_value'=>'yes_area_leader' again returns everyone as area leaders. There is a meta_value of that, but it doesn't seem to register it with that particular user that its associated with in the database.

Having both 'meta_key'=>'area_leader' and 'meta_value'=>'yes_area_leader' inside the array, and no-one is an area leader.

This is confusing :) In my mind, that should work..but it doesn't.

Oh, and you have a syntax error in that line, you have an extra ' in there ;)

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4441995 posted 3:37 pm on Apr 24, 2012 (gmt 0)

I told you it was typed on the fly. :-)

Are you flushing it out inside the loop?

$is_leader = $leader_check = null;

If you don't, $is_leader will stay set on the first iteration found.

What happens here?

if (!empty($is_leader)) { echo '<strong>Area Leader:</strong> ' . $is_leader . ' '; }

Maybe it needs to be like this?

if ($is_leader) {}

Do an experiment to get **just** the list of area leaders by meta value. Like


wp_user_search = new WP_User_Query(array('meta_key'=>'area_leader'));
$leader_check = $wp_user_search->get_results();
if (!empty($leader_check)){
echo '<ul>';
foreach ($leader_check as $leader) {
echo '<li>' . $info->first_name.' '.$info->last_name.'</li>';
}
echo '</ul>';
}


If it displays all members, leader or not, maybe you need to add the value of 1:

array('meta_key'=>'area_leader','meta_value'=>1)

You're close though, it's probably something simple and unseen.

surrealillusions

5+ Year Member



 
Msg#: 4441995 posted 4:22 pm on Jun 26, 2012 (gmt 0)

Time to dig this up from the depths...well, sort of.

I've come back to this project as now it needs to be worked on more.

I've had a try at getting this area leader sorted, but still nothing. Here's what I have, but it either echos the else statement, or all the users for the area, or the last person in the area alphabetically and echos them out twice...depending on how I write the array. With 0 or 1, or without the meta_value part, or without the meta_key part. Tried all the combinations I think, but to no avail.

I've set up a few test accounts, with 3 users in the same area, but only 1 is the area_leader.


$wp_user_search = new WP_User_Query(array('meta_key'=>'area_leader','meta_value'=>0));
$leader_check = $wp_user_search->get_results();
if (!empty($leader_check)){
foreach ($leader_check as $leader) {
echo "<h3>Area Leader</h3>";
echo "<p>Your area leader is: " . $info->first_name . ' ' . $info->last_name . "</p>";
}
}
else { echo '<p>There is no area leader for...</p>'; }

surrealillusions

5+ Year Member



 
Msg#: 4441995 posted 4:26 pm on Jun 26, 2012 (gmt 0)

Also, I tried print_r on $leader_check, and it comes back full of information about the user, but only from wp_users table, and not the wp_usermeta where the area_leader stuff is located.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4441995 posted 4:13 pm on Jun 27, 2012 (gmt 0)

What is "$info?" Don't you want

$leader->first_name . ' ' . $leader->last_name

I'm also guessing the meta_value should be a 1, too.

surrealillusions

5+ Year Member



 
Msg#: 4441995 posted 5:17 pm on Jun 27, 2012 (gmt 0)

Ahh..the $info was left over from the previous statement of the all member list of that area, so it was conflicting.

There was also a line missing. This now, seems to work:

$wp_user_search = new WP_User_Query(array('meta_value'=>'yes_area_leader'));
$leader_check = $wp_user_search->get_results();
// print_r ($leader_check);

if (!empty($leader_check)){
foreach ($leader_check as $leader) {
$leaderinfo = get_userdata($leader->ID);
echo "<h3>Area Leader</h3>";
echo "<p>Your area leader is: " . $leaderinfo->first_name . ' ' . $leaderinfo->last_name . "</p>";
}
}
else { echo '<p>There is no area leader for...</p>'; }

surrealillusions

5+ Year Member



 
Msg#: 4441995 posted 5:43 pm on Jun 27, 2012 (gmt 0)

Maybe it doesn't work then...it displays all area leaders, and not the one specific to that area.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4441995 posted 3:54 pm on Jun 28, 2012 (gmt 0)

Do an experiment to get **just** the list of area leaders by meta value.


OK! Now review post #4443051, you just need to add the user's area to the query to select the one that's this user's leader.

surrealillusions

5+ Year Member



 
Msg#: 4441995 posted 8:35 pm on Jul 4, 2012 (gmt 0)

I think I have what I need now.

Thanks for your help, everyone :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / WordPress
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved