Title: List from custom field
Last modified: August 20, 2016

---

# List from custom field

 *  [mike14017](https://wordpress.org/support/users/mike14017/)
 * (@mike14017)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/list-from-custom-field/)
 * Hi
 * What I am trying to achieve is produce a page with a list taken from a custom
   field, I then want the list to be clickable to show all the posts linked to that
   custom fireld.
 * I can produce the list using
 *     ```
       <?php
         $metakey = 'merchant';
         $merchants = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
         if ($merchants) {
           foreach ($merchants as $merchant) {
             echo "$merchant";
       	  echo "<br />";
           }
         }
         ?>
       ```
   
 * So far so good, what I can’t work out is how to make them clickable to show the
   posts. I’ve spent hours looking through the forum and the net but I am no closer.
   If anybody could point me in the right direction I would appreciate it.
 * Thanhks
 * Mike

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/list-from-custom-field/#post-2235868)
 * What is in $merchant? What do you want each value to link to?
 *  Thread Starter [mike14017](https://wordpress.org/support/users/mike14017/)
 * (@mike14017)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/list-from-custom-field/#post-2235893)
 * Hi
 * $merchant is the name of the custom field I want to use. The script above will
   produce a list of every name in this field in alphabetical order. What I also
   want is when a visitor clicks on a name it will show every post associated/linked
   with that name/custom field.
 * I hope that explains it.
 * Thanks
 * Mike
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/list-from-custom-field/#post-2235936)
 * I assume that $merchant contains the name of a merchant. One way to do what you
   want is to add a Page that will show your list of posts for one merchant and 
   [create a template](http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates)
   to retrieve the merchant name from the query string.
 * Then, assuming the URL to your Page is ‘[http://mysite.com/my-merchant-page&#8217](http://mysite.com/my-merchant-page&#8217);,
   you would modify the code you show like this:
 *     ```
       <?php
         $metakey = 'merchant';
         $merchants = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
         if ($merchants) {
           foreach ($merchants as $merchant) {
             echo '<a href="http://mysite.com/my-merchant-page/?merchant=' . urlencode($merchant) . "\" alt='merchant' title='$merchant'>$merchant</a>";
       	  echo "<br />";
           }
         }
         ?>
       ```
   
 * In your template, get the merchant name like this:
 *     ```
       $merchant = (isset($_GET['merchant'])) ? urldecode($_GET['merchant']) : 'default merchant';
       ```
   
 * If $merchant has the default value, do whatever you want to handle it. Otherwise,
   use that $merchant in your query to get the posts.
 * None of this code has been tested, so check carefully for typos or other errors.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘List from custom field’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/list-from-custom-field/#post-2235936)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
