WordPress.org

Ready to get started?Download WordPress

Forums

List unique value of custom field (23 posts)

  1. Rakesh Raja
    Member
    Posted 2 years ago #

    Hi

    I have a custom field and I want to list only unique value of that only how can I do that?

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

  3. Rakesh Raja
    Member
    Posted 2 years ago #

    Can you tell me how this will help? Already checked this functions.

    My custom field name is "judge"

    There are lots posts. And when in I put custom field code in loop it show same value multiple times

    What I want is JUST A CODE. Which display unique values just not duplicate

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    So just list one post with this meta value in your loop.

  5. Rakesh Raja
    Member
    Posted 2 years ago #

    Esmi.. There will be lots post and each post will have different judges
    if i will list just one post.. i will get just one judge.

    I want to list all unique Judge names :)

  6. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    <?php echo get_post_meta($post->ID,'judge',true); ?>

    if that does not work, you need to paste the full code of your template into a http://pastebin.com/and post the link to it here, so somenone can have a look at it to see where to add the meta value output.

    it might help if you can post a link to your site to illustrate what is hapening right now.

  7. esmi
    Forum Moderator
    Posted 2 years ago #

    Then list all posts that have the custom field - irrespective of the field's value.
    http://codex.wordpress.org/Function_Reference/WP_Query

  8. Rakesh Raja
    Member
    Posted 2 years ago #

    Here is the code of the page

    http://pastebin.com/whnBCR6T

    Here is the dev url of the page.. You can see duplicate names

    http://wpguru.in/test/judges/

    regards

  9. Rakesh Raja
    Member
    Posted 2 years ago #

    Esmi here is the code you suggested its still not working

    http://pastebin.com/UY2r8XF2

    this paste has both my code and yours

    Let me clear once again I just want to list "UNIQUE VALUE" of a custom field called "judge"

    I dont want to list post title or post content.. Just unique value of custom field called "Judge"

  10. esmi
    Forum Moderator
    Posted 2 years ago #

    And what's the problem with the second Loop?

  11. Rakesh Raja
    Member
    Posted 2 years ago #

    Check online http://wpguru.in/test/judges/

    it did same thing. Duplicate values

    Regardless of what I did can you paste your own code for listing unique values of custom field "judge"?

  12. Rakesh Raja
    Member
    Posted 2 years ago #

    It did list posts with that custom field only but the problem is that custom field will have duplicate values. But i want to list only unique values.

  13. esmi
    Forum Moderator
    Posted 2 years ago #

    Then we're back to just listing one post for each meta value, yes?

  14. Rakesh Raja
    Member
    Posted 2 years ago #

    Humm how do I explain? Let me try again

    (1) I want to list ONLY unique values
    (2) I want to list ALL values of that custom field
    (3) I DONT WANT to list any post data like title or post text.

  15. esmi
    Forum Moderator
    Posted 2 years ago #

    Use a query to grab all posts with a value entered for your chosen field. Loop through those posts building up an array of values for the custom field. Display the final array in any manner that suits your needs

  16. Rakesh Raja
    Member
    Posted 2 years ago #

    As i told there will be multiple values for that field and I just want list unique ones. What you suggest is just to use a value myself

    Let me also explain when I say unique I mean value that list only once.

    Like

    Post 1 has value "esmi"
    Post 2 has value " Rakesh"
    Post 3 has value " Esmi"

    So what i want is to list unique values like "rakesh" and "esmi"

    What I am getting now is

    Esmi
    Rakesh
    Esmi

  17. esmi
    Forum Moderator
    Posted 2 years ago #

    Use in_array() to test to see if you have added a given meta-value to your array before adding it.

  18. Rakesh Raja
    Member
    Posted 2 years ago #

    http://pastebin.com/UY2r8XF2

    If you look in the code and do that for me than great. I am not well versed in that.

    I am just a designer who copy paste codes from here and there and make things work :)

  19. Rakesh Raja
    Member
    Posted 2 years ago #

    I got an idea.. can you tell me how to access values from database directly?

    no loop.. just a mysql code

  20. Rakesh Raja
    Member
    Posted 2 years ago #

    Hi

    I have done it myself with pure mysql query..

    http://pastebin.com/QrdMBrhq

    can you tell me how can I achieve this by using $wpdb?

    regards

  21. hiraiqbal
    Member
    Posted 2 years ago #

    hi rakeshraja,
    did you find solution in wpdb?
    I am searching for this.
    I am having custom field 'city' for multiple posts,
    and I need to display distinct city names with posts count,
    e.g:London (13)
    Paris (10)
    where 13 and 10 are number of posts means there are 13 posts having custom field 'city' value as 'London'.
    what will be the query?

    $query = new WP_Query(array('post_type'=>'post','cat'=>'17','meta_key'=>'City','groupby'=>'meta_value'));
    did not worked.
    any help?
    how to use posts_groupby?

  22. hotwebideas
    Member
    Posted 1 year ago #

    rakeshraja, this may be a little late, but here's a solution:

    Use the custom loop links that was sent by esmi, but add an array for all your returned custom field values, including ALL of your duplicate fields.

    After that loop is done, use array_unique function to return the unique value. Now, you have your unique values.

    A little PHP goes a LONGGGGG way, LOL!

    Bruce

  23. asif_2001
    Member
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Topic