How to show meta_values from specific posts

I have tried this code:

<select name="productSize" id="productSize">
    <?php
        $metakey = 'size';
        $sizes = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
        if ($sizes) {
            foreach ($sizes as $size) {
                echo "<option value="" . $size . "">" . $size . "</option>";
            }
        }
    ?>
</select>

But it shows all values with ‘size’ key from all posts. I want to show only the values which associated on each posts. How to do that?

Read More

Thank you, really appreciate your help.

Related posts

Leave a Reply

1 comment

  1. Alter $post->ID if required, but by default it will load the current loaded post ID.

    <select name="productSize" id="productSize">
        <?php
            global $post;
            $metakey = 'size';
            $sizes = get_post_meta($post->ID, $metakey, false);
            if (count($sizes) != 0) {
                foreach ($sizes as $size) {
                echo "<option value="" . $size . "">" . $size . "</option>";
                }
            }
        ?>
    </select>