Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to display custom lists of users WordPress

  • SOLVED

Hi there

I have added several custom fields to my user profiles (via functions.php), including 'ward' and 'electedmember'. These all display fine on my author pages, but I wish to produce pages for each ward. On these pages I would like to display a list of users who match a certain ward ('ward'='northgate') and a certain type of elected member ('electedmember'='citycouncillor'). This being so I may see who are the city and county councillors for that ward only.
Can this be done, and how (plugin or within the theme)? (Preference for the plugin)

Also, is it possible to pull the ward reference (northgate) from the page title, or will I have to have separate templates for each ward?

Any help is greatly appreciated.

Many thanks

Allen :-)

Answers (1)

2010-08-19

MagoryNET answers:

My two methods:

Method I. SPECIAL PAGE TEMPLATE

Create a template for a special page in your theme directory (for ward.php file):

<?php
/*
Template Name: Ward Page
*/

get_header();

// here display your list, you will probably have to close it
// with <div id="wrapper"> or something, depending on your theme

global $wpdb;
$ward = $wpdb->escape($_GET['ward']);
$electedmember = $wpdb->escape($_GET['electedmember']);

$q = "SELECT ID FROM wp_users u, wp_usermeta m1, wp_usermeta m2 WHERE u.ID = m1.user_id AND u.ID = m2.user_id AND m1.meta_key = 'ward' AND m1.meta_value = '$ward' AND m2.meta_key = 'electedmember' AND m2.meta_value = '$electedmember' LIMIT 3";
$res = $wpdb->get_results($q);

if($res)
{
echo "<ul>";
foreach($res as $uid)
{
$userid = $uid->ID;
$user_info = get_userdata($userid);

echo "<li>";
echo '<a href="/author/'.$user_info->user_nicename.'">'.$user_info->user_login.'</a>';
echo "</li>";
}
echo "</ul>";
}
get_footer();



Create a page that uses this template (for example /wardpage/).
You can now display your list like that:

http://your.site.com/wardpage/?ward=northgate&electedmember=citycouncillor

In .htaccess you can define a simpler link for this adding a line just after RewriteEngine On:

RewriteRule ^wardpage/(.+)/(.+) index.php/wardpage/?ward=$1&electedmember=$2 [L]

And then use urls like that:

http://your.site.com/wardpage/northgate/citycouncillor

You will probably need to add get_sidebar() somewhere depending on the theme you are using - you can copy fragments of page.php to make sure it fits the theme.

Method II. AS A PLUGIN

1. Create an empty page "ward" (or anything you like)
2. Create a folder wardplugin in wp-content/plugins
3. create index.php in the folder:


<?php
/**
* @package WardPlugin
* @author Tomasz Kucza
* @version 1.0.0
*/
/*
Plugin Name: WardPlugin
Plugin URI: http://magory.net/
Description: A plugin for displaying user lists with special usermeta
Author: Tomasz Kucza
Version: 1.0.0
Author URI: http://magory.net
*/

add_filter('the_content', 'wardUsers');

function wardUsers($content)
{
global $wpdb, $post;
if($post->post_name != 'ward') // change it if your page has different post_name
return $content;

$ward = $wpdb->escape($_GET['ward']);
$electedmember = $wpdb->escape($_GET['electedmember']);
$q = "SELECT ID FROM wp_users u, wp_usermeta m1, wp_usermeta m2 WHERE u.ID = m1.user_id AND u.ID = m2.user_id AND m1.meta_key = 'ward' AND m1.meta_value = '$ward' AND m2.meta_key = 'electedmember' AND m2.meta_value = '$electedmember' LIMIT 3 ";
$res = $wpdb->get_results($q);
if($res)
{
$content.="<ul>";
foreach($res as $uid)
{
$userid = $uid->ID;
$user_info = get_userdata($userid);
$content.="<li>";
$content.='<a href="/author/'.$user_info->user_nicename.'">'.$user_info->user_login.'</a>';
$content.="</li>";
}
$content.="</ul>";
}
return $content;
}


4. Activate the plugin.

I didn't test the plugin version, so it could have some bugs. I hope I didn't make any mistake and I understood what you want to do right. :)


Allen Tullett comments:

Hi MagoryNET

I am a little confused as to how I would use this to display a list on an existing page. I say this as within an existing template I wish to display a list of the city councillors (citycouncillor) and a list of county councillors (countycouncillor), both only containing at most 3 entries. Is this possible with this script, or am I getting confused.

Also, is it possible to add permalinks to the author pages? .../author/...

Sorry, my mind is not with me today and I should have put this earlier.

Many thanks

Allen


MagoryNET comments:

Where exactly do you want to display this list? On a special page dedicated to the list (just like "author" pages are dedicated for authors) or in sidebar as a widget?

Method I displays the list on a separate page (but doesn't allow anything else to be displayed there). So you have a page on your site where you have the list.
Method II allows you adding a content to the page - above the list.

I edited the answer a little to add permalinks and the limit. But I don't understand what exactly do you want to display. :)


Allen Tullett comments:

An example page is [[LINK href="http://www.allentullett.co.uk/wards/northgate-ward/"]]http://www.allentullett.co.uk/wards/northgate-ward/[[/LINK]], this is using a template called 'ward_template.php'.
Within the blue bar are names listed of councillors (at present displaying custom field information) that should link through to their respective author pages.

For each ward page (a dozen or so) I would like this list to instead display a those users/authors who relate to that ward, and are either a city or county councillor.

:-)


MagoryNET comments:

Could you provide ward_template.php source code? I understand know what needs to be . :)


Allen Tullett comments:

Hi

Many thanks for this. Excuse my messy template, I'm learning ways to improve it as I go along. For now it works.
The section requiring changing is 'Councillors & Surgery Information' (about 1/3 way down), I have inserted notes. I assume it would be a custom query of some kind but you know best.
I did think last night, that the reference for the ward could instead come from a custom field if the page title is too difficult to extract.


<?php
/*
Template Name: Ward
*/
?>
<?php get_header(); ?>

<div id="leather">

<div id="content-container" class="clearfloat">

<div class="wrapper clearfloat">
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php
if ($children) { ?>
<div class="submenu">
<h4 class="pagetitle">In this section:</h4>
<ul class="subnav">
<?php echo $children; ?>
</ul>
</div>

<?php } ?>

<?php wp_link_pages('before=<p>&after=</p>&next_or_number=number&pagelink=page %'); ?>

<div id="small-blue-title">
<?php
$parent_title = get_the_title($post->post_parent);?>
<?php echo $parent_title;?>
<?php echo $children;?>
</div>

<div id="title">
<?php the_title(); ?>
</div>

<div id="page_content">

<div id="ward-info-container">

<div id="sub-small-blue-title">
Councillors & Surgery Information
</div>

<div id="ward-info-container-blue">

<div id="ward-info-block-left">

<div id="ward-info-block-title">
City Councillors
</div>

<div id="ward-info-block-links">

// insert list of 'citycouncillor' + 'northgate' users here

</div>

</div><!--END INFO BLOCK LEFT-->

<div id="ward-info-block-left">

<div id="ward-info-block-title">
County Councillor
</div>

<div id="ward-info-block-links">

// insert list of 'countycouncillor' + 'northgate' users here

</div>

</div><!--END INFO BLOCK LEFT-->

<div id="ward-info-block-right">

<div id="ward-info-block-title">
Surgery Information
</div>

<div id="ward-info-block-links">
<?php echo get_post_meta($post->ID, 'Surgery-Info', TRUE); ?><br>
</div>

</div><!--END INFO BLOCK RIGHT-->



</div><!--END WARD INFO CONTAINER BLUE-->

</div><!--END WARD INFO CONTAINER-->


<?php the_content(); ?>

// note for self - extend into category listing here

<div id="edit">
<?php edit_post_link(__("+ Edit this page"), ''); ?>
</div>

<div id="page-parent-link">
<?php $parent_title = get_the_title($post->post_parent);?><a href="<?php echo get_permalink($post->post_parent) ?>">+ Go to <?php echo $parent_title;?></a> <?php echo $children;?>
</div>

</div>

</div>
<!--END POST-->
<?php endwhile; endif; ?>

</div>
<!--END COLUMN-->

</div>
<!--END CONTAINER-->

</div>
<!--END FLOATS-->

<?php get_footer(); ?>


Sorry for all the confusion.

Allen


MagoryNET comments:

Modified template:

Changes made:
1. getUseList function at the beginning.
2. In "insert list of" places I adedd getUserList('citycouncillor'); and getUserList('countycouncillor');


<?php
/*
Template Name: Ward Page
*/

function getUserList($type)
{
global $wpdb, $post;
$req = $_SERVER['REQUEST_URI'];
$pos = strpos($req, 'wards/')+strlen('wards/');
if($pos!==FALSE)
{
$ward = trim(substr($req, $pos), '/');
$ward = $wpdb->escape($ward);
echo " $ward $type <br/>";
$q = "SELECT ID FROM wp_users u, wp_usermeta m1, wp_usermeta m2 WHERE u.ID = m1.user_id AND u.ID = m2.user_id AND m1.meta_key = 'ward' AND m1.meta_value = '$ward' AND m2.meta_key = 'electedmember' AND m2.meta_value = '$type' LIMIT 3 ";
$q = "SELECT ID FROM wp_users u, wp_usermeta m1, wp_usermeta m2 WHERE u.ID = m1.user_id AND u.ID = m2.user_id AND m1.meta_key = 'rich_editing' AND m1.meta_value = 'false' AND m2.meta_key = 'rich_editing' AND m2.meta_value = 'false' LIMIT 3 ";
$res = $wpdb->get_results($q);
if($res)
{
foreach($res as $uid)
{
$userid = $uid->ID;
$user_info = get_userdata($userid);
$current_link = get_author_posts_url($userid,$user_info->display_name);
echo '<a href="'.$current_link.'">'.$user_info->display_name.'</a><br>';
}
}
}
}
?>
<?php get_header(); ?>

<div id="leather">

<div id="content-container" class="clearfloat">

<div class="wrapper clearfloat">
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php
if ($children) { ?>
<div class="submenu">
<h4 class="pagetitle">In this section:</h4>
<ul class="subnav">
<?php echo $children; ?>
</ul>

</div>

<?php } ?>

<?php wp_link_pages('before=<p>&after=</p>&next_or_number=number&pagelink=page %'); ?>

<div id="small-blue-title">
<?php
$parent_title = get_the_title($post->post_parent);?>
<?php echo $parent_title;?>
<?php echo $children;?>
</div>

<div id="title">
<?php the_title(); ?>
</div>

<div id="page_content">

<div id="ward-info-container">

<div id="sub-small-blue-title">
Councillors & Surgery Information
</div>

<div id="ward-info-container-blue">

<div id="ward-info-block-left">

<div id="ward-info-block-title">
City Councillors
</div>

<div id="ward-info-block-links">

<?php
// insert list of 'citycouncillor' + 'northgate' users here
getUserList('citycouncillor');
?>

</div>



</div><!--END INFO BLOCK LEFT-->

<div id="ward-info-block-left">

<div id="ward-info-block-title">

County Councillor

</div>

<div id="ward-info-block-links">

<?php
// insert list of 'countycouncillor' + 'northgate' users here
getUserList('countycouncillor');
?>

</div>

</div><!--END INFO BLOCK LEFT-->

<div id="ward-info-block-right">

<div id="ward-info-block-title">

Surgery Information

</div>

<div id="ward-info-block-links">

<?php echo get_post_meta($post->ID, 'Surgery-Info', TRUE); ?><br>

</div>

</div><!--END INFO BLOCK RIGHT-->



</div><!--END WARD INFO CONTAINER BLUE-->


</div><!--END WARD INFO CONTAINER-->


<?php the_content(); ?>

// note for self - extend into category listing here

<div id="edit">

<?php edit_post_link(__("+ Edit this page"), ''); ?>

</div>



<div id="page-parent-link">

<?php $parent_title = get_the_title($post->post_parent);?><a href="<?php echo get_permalink($post->post_parent) ?>">+ Go to <?php echo $parent_title;?></a> <?php echo $children;?>

</div>



</div>

\

</div>

<!--END POST-->

<?php endwhile; endif; ?>


</div>
<!--END COLUMN-->

</div>

<!--END CONTAINER-->



</div>

<!--END FLOATS-->



<?php get_footer(); ?>


MagoryNET comments:

Sorry my mistake. I forgot to delete one line added for testing.


<?php
/*
Template Name: Ward Page
*/

function getUserList($type)
{
global $wpdb, $post;
$req = $_SERVER['REQUEST_URI'];
$pos = strpos($req, 'wards/')+strlen('wards/');
if($pos!==FALSE)
{
$ward = trim(substr($req, $pos), '/');
$ward = $wpdb->escape($ward);
echo " $ward $type <br/>";
$q = "SELECT ID FROM wp_users u, wp_usermeta m1, wp_usermeta m2 WHERE u.ID = m1.user_id AND u.ID = m2.user_id AND m1.meta_key = 'ward' AND m1.meta_value = '$ward' AND m2.meta_key = 'electedmember' AND m2.meta_value = '$type' LIMIT 3 ";
$res = $wpdb->get_results($q);
if($res)
{
foreach($res as $uid)
{
$userid = $uid->ID;
$user_info = get_userdata($userid);
$current_link = get_author_posts_url($userid,$user_info->display_name);
echo '<a href="'.$current_link.'">'.$user_info->display_name.'</a><br>';
}
}
}
}
?>
<?php get_header(); ?>

<div id="leather">

<div id="content-container" class="clearfloat">

<div class="wrapper clearfloat">
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php
if ($children) { ?>
<div class="submenu">
<h4 class="pagetitle">In this section:</h4>
<ul class="subnav">
<?php echo $children; ?>
</ul>

</div>

<?php } ?>

<?php wp_link_pages('before=<p>&after=</p>&next_or_number=number&pagelink=page %'); ?>

<div id="small-blue-title">
<?php
$parent_title = get_the_title($post->post_parent);?>
<?php echo $parent_title;?>
<?php echo $children;?>
</div>

<div id="title">
<?php the_title(); ?>
</div>

<div id="page_content">

<div id="ward-info-container">

<div id="sub-small-blue-title">
Councillors & Surgery Information
</div>

<div id="ward-info-container-blue">

<div id="ward-info-block-left">

<div id="ward-info-block-title">
City Councillors
</div>

<div id="ward-info-block-links">

<?php
// insert list of 'citycouncillor' + 'northgate' users here
getUserList('citycouncillor');
?>

</div>



</div><!--END INFO BLOCK LEFT-->

<div id="ward-info-block-left">

<div id="ward-info-block-title">

County Councillor

</div>

<div id="ward-info-block-links">

<?php
// insert list of 'countycouncillor' + 'northgate' users here
getUserList('countycouncillor');
?>

</div>

</div><!--END INFO BLOCK LEFT-->

<div id="ward-info-block-right">

<div id="ward-info-block-title">

Surgery Information

</div>

<div id="ward-info-block-links">

<?php echo get_post_meta($post->ID, 'Surgery-Info', TRUE); ?><br>

</div>

</div><!--END INFO BLOCK RIGHT-->



</div><!--END WARD INFO CONTAINER BLUE-->


</div><!--END WARD INFO CONTAINER-->


<?php the_content(); ?>

// note for self - extend into category listing here

<div id="edit">

<?php edit_post_link(__("+ Edit this page"), ''); ?>

</div>



<div id="page-parent-link">

<?php $parent_title = get_the_title($post->post_parent);?><a href="<?php echo get_permalink($post->post_parent) ?>">+ Go to <?php echo $parent_title;?></a> <?php echo $children;?>

</div>



</div>

\

</div>

<!--END POST-->

<?php endwhile; endif; ?>


</div>
<!--END COLUMN-->

</div>

<!--END CONTAINER-->



</div>

<!--END FLOATS-->



<?php get_footer(); ?>


Allen Tullett comments:

Ah ha, it display the user, and a permalink (of which I need to edit the htaccess as you suggest).

Is it possible to reference a second ward value ('ward2'), whereby one councillor may cover more than one ward. As I notice that I cannot put it into the user meta 'ward'.

For instance - Graham covers Northgate (ward) and St Stephens (ward2), how can he be displayed in both?

:-)


MagoryNET comments:

If you named the user fields ward/ward2 the getUseList function like below:


function getUserList($type)

{

global $wpdb, $post;

$req = $_SERVER['REQUEST_URI'];

$pos = strpos($req, 'wards/')+strlen('wards/');

if($pos!==FALSE)

{

$ward = trim(substr($req, $pos), '/');

$ward = $wpdb->escape($ward);

echo " $ward $type <br/>";

$q = "SELECT ID FROM wp_users u, wp_usermeta m1, wp_usermeta m2 WHERE u.ID = m1.user_id AND u.ID = m2.user_id AND ( m1.meta_key = 'ward' OR m1.meta_key = 'ward2' ) AND m1.meta_value = '$ward' AND m2.meta_key = 'electedmember' AND m2.meta_value = '$type' LIMIT 3 ";

$res = $wpdb->get_results($q);

if($res)

{

foreach($res as $uid)

{

$userid = $uid->ID;

$user_info = get_userdata($userid);

$current_link = get_author_posts_url($userid,$user_info->display_name);

echo '<a href="'.$current_link.'">'.$user_info->display_name.'</a><br>';

}

}

}

}


Allen Tullett comments:

By god you got it!!!!!!!!!

Thank you for that. One quick question before I close this. Can I clean up the author url. The one generated shows as:
..../Julian%20Brazier%20TD/

...and if I go via a category page it gives me:
..../julian-brazier-td/

Both of which work, but is it possible to receive the cleaner version. If not don't worry.

:-)


MagoryNET comments:

After the line

$current_link = get_author_posts_url($userid,$user_info->display_name);


add


$current_link = str_replace('%20', '-', strtolower($current_link));


Should work but it's not a clean solution. Probably there is a better method.


Allen Tullett comments:

Its interesting that the link appears clean, but the page still shows %20
Plus it has lowercased everything, of which I prefer, but odd!
Any thoughts


MagoryNET comments:

%20 is a space. I assumed it's already changed to %20 in the url but it's probably just a space. So change the added line of code to:


$current_link = str_replace(' ', '-', strtolower($current_link));


Allen Tullett comments:

By jove you got it!

Many many thanks. You're fab!