[Resolved] Looking to Sort Custom Fields by Meta Data Values
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:
I want to do something like this (pseudocode):
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?
Got it to sort by one meta_key value using this syntax:
But I really need it to sort by month, day and year. Doesn’t seem to be possible using simple query_posts().
If you don’t order by meta_value it will be sorted, by default, by date.
Meaning, just use
meta_value=to filter, and don’t use the orderby clause.
That’s the thing, if I just use something like:
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.
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 😉
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).
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 😉
OK, thanks. (Your anchor tag in the above post appears to be a bit messed up.)
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.
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.
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.
I posted the code for the calendar with a little explanation on how it works over here.
Could anyone explain how I can sort by meta_key=surname and then alphabetically by meta value asc ?
- The topic ‘[Resolved] Looking to Sort Custom Fields by Meta Data Values’ is closed to new replies.