[resolved] List all custom field values in archive page (3 posts)

  1. Stefano
    Posted 3 years ago #

    I have a custom post type PRODUCT with a custom field called COLOR.

    How can i list all the COLOR values in the archive page?

    Basically I want to be able to have a list of all colors associated to products in the current archive, so I can filter them also based on a specific color, but only if the COLOR is associated at least to a PRODUCT in the specified archive (PRODUCT archive, but also in a CATEGORY archive, TAG archive, etc...)

    Thanks a lot!

  2. bcworkz
    Posted 3 years ago #

    You can get all unique color values in the postmeta table with a SQL query like SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key = 'color';

    If other post types have a color field, or you only want colors from a certain date range, etc., you need to join the posts table to apply more criteria, but the idea is the same.

    Alternately, you could get the color value for each post in the archive loop and push it into an array. Then use array_unique() to get rid of duplicates.

  3. Stefano
    Posted 2 years ago #

    I ended up implementing the COLOR as a custom taxonomy. Now everything is easier ;)

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.