• 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)
  • What is in $merchant? What do you want each value to link to?

    Thread Starter mike14017

    (@mike14017)

    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

    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 to retrieve the merchant name from the query string.

    Then, assuming the URL to your Page is ‘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.