How can I spit out a list of values of a custom field, suppressing duplicate values?

More specifically...

For custom post named "pressmention", I want to output a file, screen echo or web page of values in its custom text field named "press_mention_information_publication-name".

However, the output should only spit out a value once.

That is to say, "pressmention" has approximately 70,000 posts. For a quantity of those (let's say, 1,000), "press_mention_information_publication-name" may have value "Wall Street Journal". But I would only want to see "Wall Street Journal" output once.

This is not for a front-end feature, it does not need to exist on WordPress. It is for my information.


Here's a dashboard widget you could manage them with:

// add dashboard widget
add_action('wp_dashboard_setup', 'wpse_dashboard_widget');
function wpse_dashboard_widget() {
    // only show widget to users who can publish pages
    if(current_user_can('publish_pages')) {
        wp_add_dashboard_widget('deduped_dash_widget', 'Publications', 'wpse_create_deduped_dash_widget');
// widget contents
function wpse_create_deduped_dash_widget() {
    // select publications without duplicates by using DISTINCT
    global $wpdb;
    $mentions = $wpdb->get_results("SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key = 'press_mention_information_publication-name'");
    // if any were found
    if($mentions) {
        // display them in an unordered list; could do <ol> if you want numbers
        echo '<ul>';
        foreach($mentions as $publication) {
            echo '<li>' . $publication->meta_value) . '</li>';
        echo '</ul>';

If you have a custom theme or custom child theme you can place this in functions.php. Otherwise, you can create a plugin which may be easier than creating a child theme just for this widget.

Or, if you have access to phpMyAdmin, you could run the query

SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key = 'press_mention_information_publication-name'

and get the list as well.

| improve this answer | |
  • The SQL works, but the dashboard widget throws an error, here it is via debug logging... Parse error: syntax error, unexpected ')', expecting ',' or ';' in /home/sitename/public_html/wp-content/themes/blankslate/functions.php on line 277 Yes, I hve a plugin in iamp-index-management. – Robert Andrews May 26 '17 at 21:20
  • I have fixed part of the error by removing the rogue close-bracket. Now I get to see the dashboard. But this produces a few more error messages... - one in debug loggin Notice: Use of undefined constant wpse_create_deduped_dash_widget - assumed 'wpse_create_deduped_dash_widget' in /home/braincloud/public_html/wp-content/themes/blankslate/functions.php on line 265 and two within the dashboard widget: Notice: Undefined variable: wpdb in /home/sitename/public_html/wp-content/themes/blankslate/functions.php on line 271 – Robert Andrews May 26 '17 at 21:26
  • Fatal error: Call to a member function get_results() on a non-object in /home/sitename/public_html/wp-content/themes/blankslate/functions.php on line 271 Sorry I haven't figured out how to line-break in a comment. – Robert Andrews May 26 '17 at 21:26
  • 1) I fixed the error message about wpse_create_deduped_dash_widget by putting single quotes around both ends. 2) But there was still an error about an undefined $wpdb. I added global $wpdb;, can you confirm that's right? 3) That left an error from SQL output within your dashboard widget box concerning not finding the post_meta. So I tweaked the SQL query. 4) I have working code at a fiddle URL, though not sure rules permit me to link. – Robert Andrews May 26 '17 at 21:45
  • @Robert: I've updated the answer - yes, a quote was missing and you do need global $wpdb. You should be able to link to a fiddle URL here and I'm happy to update the answer with the updates, sorry it didn't work as-is but to hear glad you got it working. – WebElaine May 30 '17 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.