[wp-hackers] list users by (dynamic) meta value

Gavin Pearce Gavin.Pearce at 3seven9.com
Tue Oct 23 10:16:02 UTC 2012


Hey Lee,

Sorry - didn't follow 100%, perhaps it's me!

Just to summarise, you have the following 'pages' setup in WordPress?

Actors
Directors
Agents 
Etc

On each page, you want to list all users that have a meta value that
matches the above?

You could use the page name (essentially slug/URL), though this seems a
bit `dirty`.

Personally I'd do the following:

- Create a custom field on each *page*, called 'display-team' or
something similar (but different so as not to confuse yourself!).

- On each WP page, run the query as follows:

$args = array(
			'role' => 'author' ,
			'meta_key' => 'team',
			'meta_value' => get_post_meta(get_the_ID(),
'display-team', true), <~ THIS NEEDS TO BE DYNAMIC 'order' => 'ASC',
'number' => 25, 
		); 
		
$authors = get_users($args);

foreach($authors as $author){
	// Do stuff
}

Note: If you're outside of the main WP loop for the page, you might need
to get post ID of the page in a different way.

So in essence:
- Create custom field on page (easily done in admin area, check Screen
Options top right if you can't see it)
- Enter same key on every page - enter value as required by which team
you want to display
- Update the query to that (requiring only one page template / sidebar)
- Thank the WordPress Gods

G


-----Original Message-----
From: wp-hackers-bounces at lists.automattic.com
[mailto:wp-hackers-bounces at lists.automattic.com] On Behalf Of A5D
Sent: 23 October 2012 10:24
To: wp-hackers at lists.automattic.com
Subject: [wp-hackers] list users by (dynamic) meta value

My head hurts.

I'm trying to work out how to list users by dynamic meta value.
The meta value can be anything.

Example:
If I am on page for directors I list the directors, If I'm on the page
for actors they get listed.
I can do this by hard coding the meta value in but then I have to
manually create a ton of pages - which kind of defeats the whole point.

What I have so far:
<?php
$args = array(
'role' => 'author' ,
'meta_key' => 'team',
'meta_value' => 'Director', <~ THIS NEEDS TO BE DYNAMIC 'order' =>
'ASC', 'number' => 25, ); $authors = get_users($args);
    foreach($authors as $author){
        $author_info = get_userdata($author->ID);
        ?>
        DO THE STUFF
        <?php } ?>

Meta values are added in the users profile.
I suppose i'm missing 2 bits - the dynamic meta value content and where
to pull that from - page title perhaps?

I appreciate any direction/ advice/ lumps of code/ slaps for bad coding
etc.

TIA

Lee
_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list