[wp-hackers] list users by (dynamic) meta value
A5D
info at a5d.org
Tue Oct 23 10:38:11 UTC 2012
Hey Gavin,
I'm presuming my cockney accent didn't come over so I'll try to explain clearer.
;)
No, I don't have a load of pages created, that's what I'm trying to avoid.
I could manually create the pages and run the query on each, individually, but then nothing will be dynamic (changes to meta_value will happen often and more pages will be required).
What I am trying to do is use one template, with a custom query, that loads a list of users based on the contents of the meta_value field.
So, I have:
Users profile with a custom meta_field:
<?php
$args = array(
'role' => 'author' ,
'meta_key' => 'team',
'meta_value' => 'CONTENTS OF THIS FIELD DICTATE THE LIST OF USERS PRESENTED',
'order' => 'ASC',
'number' => 25,
);
$authors = get_users($args);
foreach($authors as $author){
$author_info = get_userdata($author->ID);
?>
DO THE STUFF
<?php } ?>
If the meta_value = Directors then a list of directors is presented etc.
Lee
On 23 Oct 2012, at 11:16, Gavin Pearce wrote:
> 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
> _______________________________________________
> 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