[resolved] Link post together with custom metadata (4 posts)

  1. Jens Wedin
    Posted 8 years ago #

    Here's a tricky one that I have stumbled on when building a new site. The site is a whisky site where I have two categories.

    1. Distillery
    2. Single malt

    I'm using custom metadata to add data about both the single malts and the distilleries. What I want is a connection between a specific bottle and a distillery. So when a I read about a single malt bottle I want to show the custom metadata for the bottle but also the metadata from the distillery the bottle comes from.

    For example, I have a post which is called 12 year old Balvenie. The bottle comes from the the Balvenie distillery. I want to show metadata that I have added to the bottle post and also the the Balvenie distillery.

    Name: xxx (single-malt post)
    Age: xxx (single-malt post)
    Distillery: xxx (single-malt post)
    Region xxx (distillery post)
    Country: xxx (distillery post)

    Any thoughts howto do this? I'm thinking of using wpdb class, any thoughts on that?


  2. Jens Wedin
    Posted 8 years ago #

    Sorry for bumping but no one has a clue?

  3. nudnik
    Posted 8 years ago #

    I'd add some hidden posts (category = metadata, hide all metadata posts on displayed pages).

    To display the info, you'll SELECT the metadata WHERE post_title = distillery name AND category = metadata.

    It's a hack, but then again so is anything else.

    You could alternatively create pages with that info, and they'll just be private (hidden) pages. You can select by name and pull whatever info you want. Just be sure that you exclude these pages from any page listings.

    OR, if you're the only one posting (or you don't display user info anyhow), make a "user" called the distillery name, and enter the distillery info into the user boxes. (This can be confusing unless you're willing to hack the core.) I.e. "nicename" = displayed distillery name. "url" = website of distillery (if available) etc.

  4. Jens Wedin
    Posted 8 years ago #

    Thanks for the reply!

    I figured it out in another way. I used the wpdb class to query the db. This worked fine for me. Here's the code that I used.

    $distilleryvalue = get_post_meta($post->ID, $key, true);
    $distilleryvalue = $distilleryvalue . " Distillery";
    $distillery_id = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE post_title LIKE '%$distilleryvalue%'");
    $distillery_id = $distillery_id->ID;
    $distillery_meta = $wpdb->get_col("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $distillery_id");
    echo '<h2>Distillery</h2>';
    echo '<ul>';
    echo '<li>Region: ' . $distillery_meta[3] . '</li>';
    echo '<li>Country: ' . $distillery_meta[7] . '</li>';
    echo '<li><a href="' . $distillery_meta[9] . '">Official website</a>';
    echo '<li><a href="' . $distillery_meta[0] . '">Location (at Google maps)</a>';
    echo '<li><a href="' . $distillery_meta[1] . '">Buy bootles from this distillery (at the Whisky Exchange)</a>';
    echo '<li><a href="' . $distillery_meta[2] . '">Read more at Wikipedia</a>';
    echo '<ul>';

Topic Closed

This topic has been closed to new replies.

About this Topic