WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Looking to Sort Custom Fields by Meta Data Values (16 posts)

  1. theshine
    Member
    Posted 5 years ago #

    I wanted to keep track of each concert I post about, so I created some custom fields for my blog (using the Custom Field Template Plugin) that show up at the bottom each time I create a post. Those fields are: band name, day, month, year, venue, etc. Using those custom fields I created a new page (with a new calendar.php page template) called "Upcoming Show Calendar" that lives over here. The page works fine except the events are not ordered chronologically, I would like to sort them by the custom field values of "day", "month" and "year". Any ideas how I might do that? Right now the page meta data is being retrieved by this query_posts: query_posts('meta_key=band%20name&order=DESC');

    I want to do something like this (pseudocode):

    query_posts('meta_key=band%20name&order=meta_value:year&meta_value:month&meta_value:day');

    I know that syntax is wrong, but is there any way to do it? Do I need to use a wp_query rather than query_posts?

  2. MichaelH
    Member
    Posted 5 years ago #

    See the Orderby_Parameters and Custom Field Parameters of the template tag, query_posts().

  3. theshine
    Member
    Posted 5 years ago #

    Got it to sort by one meta_key value using this syntax:
    query_posts('meta_key=band%20name&orderby=meta_value&meta_key=month&order=ASC');

    But I really need it to sort by month, day and year. Doesn't seem to be possible using simple query_posts().

  4. MichaelH
    Member
    Posted 5 years ago #

    If you don't order by meta_value it will be sorted, by default, by date.

    Meaning, just use meta_key= and meta_value= to filter, and don't use the orderby clause.

  5. theshine
    Member
    Posted 5 years ago #

    That's the thing, if I just use something like:
    query_posts('meta_key=band%20name');

    Then I'm back to where I started as I don't want to order by post date (which is what I think you mean by "date"), I want to order by the date I have specified in the custom fields, which does not correspond to the order in which I am posting events.

  6. MichaelH
    Member
    Posted 5 years ago #

    Jeesh was I being dense or what?

    Can't you create one custom field with the date? Make it a whole heck of a lot easier ;)

  7. theshine
    Member
    Posted 5 years ago #

    Yeah, I guess I could do that. I was using custom drop down fields to avoid keying in invalid date format, but I can switch to a textbox, it will probably make my life easier (as long it sorts).

  8. MichaelH
    Member
    Posted 5 years ago #

    Well Hiroaki Miyashita does a good job with the Custom Field Template plugin, so I wonder if there's a way to 'validate' the date field. I put the cutom-field-template tag on this thead -- maybe Hiroaki Miyashita likely see this and have a suggestion ;)

  9. theshine
    Member
    Posted 5 years ago #

    OK, thanks. (Your anchor tag in the above post appears to be a bit messed up.)

  10. theshine
    Member
    Posted 5 years ago #

    So I got went ahead and added a date field, but unfortunately and not so surprisingly it didn't like any dates that started with numbers 10-12 and refused to pull them as part of the query. The weird thing is, is that when I went back to change the metadata to a month that starts with 1-9, it refused to pull that post again no matter what I did.

  11. theshine
    Member
    Posted 5 years ago #

    Finally figured it out. The query_posts was only bringing back 15 records (as is set in my options), it had nothing to do with the date of the show in "start date" custom field. Just so happened that the ones at the end started with larger numbers. You can check it out in all its glory over here.

  12. MichaelH
    Member
    Posted 5 years ago #

    Nice.

  13. theshine
    Member
    Posted 5 years ago #

    Hey moderator,
    If I wanted to post my calendar code somewhere on wordpress.org, what would be the best place? It's not quite a plugin, it's more of a hack or just creative use of an already existing plugin.

  14. theshine
    Member
    Posted 5 years ago #

    I posted the code for the calendar with a little explanation on how it works over here.

  15. LesleyT
    Member
    Posted 5 years ago #

    Could anyone explain how I can sort by meta_key=surname and then alphabetically by meta value asc ?

    thanks,

  16. LesleyT
    Member
    Posted 5 years ago #

    Ok, i got stuck in and sorted it with a Custom Select Query in category.php

Topic Closed

This topic has been closed to new replies.

About this Topic