Build Your Own Business Website header image

Community Library Website – Part 52 – Sort Posts by Meta Value

Difficulty Level -

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

This is the 52nd in a series of live lessons on using WordPress and Thesis as a Content Management System (CMS). In this lesson we sort the way catalog items are displayed on the Catalog Page by call number.  Call number is a custom post meta value.  We learn how to add ‘orderby’ => ‘meta-value’.  We look at both meta value and meta value numeric.
Community Library Website – Case Study Part 1 – Site Organization
Community Library Website – Case Study Part 2 – Create Custom Post Types
Community Library Website – Case Study Part 3 – Custom Post Type Configuration
Community Library Website – Case Study Part 4 – Custom Taxonomy Decisions – Theory
Community Library Website – Case Study Part 5 – Custom Taxonomy Decisions – Practical Application
Community Library Website – Case Study Part 6 – Incorporating Existing Data Into the Project
Community Library Website – Case Study Part 7 – Creating Hierarchical Custom Taxonomies
Community Library Website – Case Study Part 8 – Creating Non-Hierarchical Custom Taxonomies
Community Library Website – Case Study Part 9 – Creating Custom Post Meta
Community Library Website Case Study – Part 10a – Restore the Database Using BackupBuddy
Community Library Website Case Study- Part 10 – Creating Custom Post Meta – Continued
Community Library Website – Part 11 – Importing Existing Data to the Website
Community Library Website – Part 12 – Prepare Existing Data for import by the CSV Importer plugin
Community Library Website – Part 13 – Import Existing Data Using the CSV Importer Plugin
Community Library Website – Case Study Part 14 – Date in Permalink Configuration
Community Library Website – Part 15 – Importing Images
Community Library Website – Part 16b – Final Data Import
Community Library Website – Part 17 – Add Thesis Post Meta to Custom Post Type
Community Library Website – Part 18 – Add WordPress 3.0 Menus
Community Library Website – Part 19 – Add Post Meta and Taxonomy Information to the Post Display
Community Library – Part 20 – Add topics and audience to book display
Community Library – Part 21 – Display the Rest of the Post Meta in the Post
Community Library Website – Part 22, Create the Permalink Structure
Community Library – Part 23 – Remove Custom Post Meta from Post Display
Community Library Website – Part 24, Add Taxonomy and Post Meta Data to the Teaser
Community Library Website – Part 25, Add Taxonomy Data to the Teaser
Community Library Website – Part 26, Adjust the Teaser Styling Using Design Options
Community Library Website – Part 27, Styling the Teaser Using CSS
Community Library Website – Part 28 – Fine Tune the Teaser Display
Community Library Website – Part 29, Designing a Custom Catalog Page
Community Library Website – Part 30, Using a Query String in a URL
Community Library Website – Part 31, Adding Custom WordPress Menus
Community Library Website – Part 32, Add Custom Menus to the Catalog Page
Community Library Website – Part 33, Add Another Custom Menu to the Catalog Page
Community Library Website – Part 34, Troubleshooting Custom Menus
Community Library Website – Part 35, Add Styling to WordPress Menus
Community Library Website – Part 36, Add HTML Markup to WordPress Menus
Community Library Website – Part 37, Add WordPress Menus to Archive Pages
Community Library Website – Part 38, Add and Style a List of Tags
Community Library Website – Part 39, Add a Search Box to the Sidebar
Community Library Website – Part 40, Add Links to the Sidebar
Community Library Website – Part 41, Add Taxonomy Drill Down Browsing
Community Library Website – Part 42, Custom Loop to Display Custom Post Types
Community Library Website – Part 43, Simple Custom Query
Community Library Website – Part 44, Custom Taxonomy Query
Community Library Website – Part 45, Add Thesis HTML Structure to Teasers
Community Library Website – Part 46, Display Posts from the Last 60 Days
Community Library Website – Part 47, Custom Loop If Have Posts
Community Library Case Study – Part 48, Increase the Number of Posts that Display on the Custom Catalog
Community Library Website – Part 49 – Naming Conflicts in Custom Taxonomies
Community Library Website – Part 50 – Find and Replace Semi-colons in the Database
Community Library Website – Part 51 – Modify Taxonomy Names Directly in the Database
Community Library Website – Part 52 – Sort Posts by Meta Value
Community Library Website – Part 53 – Show the Term Label Only when a Term Exists
Community Library Website – Part 54 – Sort Archive Posts by Meta Value
Community Library Website – Part 55 – Add Sort Order to Custom Taxonomy

Video Transcript

Rick: So Pam, I’m going to turn your microphone on here because I want talk to you about sorting posts by call number.

Pam: Yes.

Rick: You wanted to sort your posts by call number?

Pam: Yes within each location. That’s the shelf order. That’s the way they’d be if you were standing in front of the shelves, looking at them. So it would be nice if we could replicate that and have them sort that way when it returns them.

Rick: And so, which pages do you want sorted that way? Or is the only kind of page you have?

Pam: Yes I have those and then I also have that default new items on the catalog page.

Rick: This one right here?

Pam: Yes, if we’re looking at the newest items.

Rick: Right. The items within the last 60 days?

Pam: Yes but I’d also like to have that… there’s 2 parts to it. I’d like to have the location themselves… those bigger groups sorted in a custom way rather than just alphabetical. And then within that location group, have them sorted by call number.

Rick: Okay. So anyway…

Pam: So then what about sorting by custom meta? In this case, the call number?

Rick: That’s what we’re going to do right now because I know I can do that right. Okay and the way we’re going to sort by custom meta… this is our catalog page right here, right? And so, what it does is it sorts out our… essentially, the way we sort this is in our query and if we go back over to this… let’s just go to WP query. Look at WP query, these are all the various arguments that are possible in WP query and you’ve seen a lot of them in the past. What we’re going to look at is the order by parameters. And so, the order by allows you to pick things like in this case, meta value. And when you specify meta value, you also have to have specified the meta key. And so, that’s what we’re going to do. We’re going to specify a meta key and we’re going to say order by meta value. And notice that it gives you 2 different ways to order by meta: by meta value or meta value number. This is the irritating thing about WordPress and maybe this is consistent in all programming languages. I don’t know but because by default, WordPress treats all numbers as if they’re letters.

And so, where you would expect a number string like this would go 1 3 4 6 34 and 56 because that’s in the order of value, the way it really does is it says 1 3 and then 34 and then 4 and then 56 and then 6 because the first letter.. the first one is the first value. It starts by that value and then it goes to the 2nd value. And so, it doesn’t actually sort the way you would expect it to sort which is why for example, some of my videos you know, number 11 video is up at the top and number 12 and then number 13 and then it goes to number 1. That’s because WordPress is not sorting it numerically. Well, you can use a numeric sort here by using the meta value num. Now, we’re going to try that with yours. I know that your call numbers are not actually entirely numeric. And so, I don’t know if that’s going to break it or not but we’re going to go ahead and try that. So first off, we’re going to add meta key. I’m just going to copy that. Go to our arguments. Remember, the last time we added post per page here? Now, we’re just going to go and add meta key. Let’s see, that’s going to be… wrap that. And then it’s not going to be keying in obviously. It’s going to be call number and I believe… let’s just go back here for a second and look at our… what do we call this thing? Get post meta ISBN… get post meta call number. So there’s the name that we need to use. We come back down here and that is our get meta key. That’s the field that we’re going to sort by. We need a comma after that one and then we add another comma and then it’s going to be order by equals greater than sign. And then we are going to order by meta…what was it? Meta num key, key number… what was that? That was meta value num. We’ll just copy that, meta value num. Now this might actually break it because it’s not a number but let’s try it and see. So we save that. That’s really all we have to do in order to make this… make that page sort that way.

We’re going to also have to do it to the other pages but let’s just first make it work here. So we upload this and then we go back to our site and we refresh it. Well, it looks like it has just ordered it by that. If we look at those numbers for a moment, let’s see. This one has a call number of 226.8… cap on. This one has a 220.6 so it’s going down. 220.7… you know, I don’t think it’s working. Yeah, actually I don’t believe that’s working because what should happen… this one… maybe I’m… yeah. 226.8, that’s the largest call number. So, this is the next one in order… 220.6 and this is 220.7. So if it was going in reverse order, this would go in place. So what we’ll do instead is we will switch that out and instead of meta value num, we’re just going to say meta value and save it again and upload it. And try that again. Okay, now we have 226.8…oh no. It’s behaving exactly the same way. Well, there you have it. You know, I’m wondering maybe what we have to do is instead of using that meta value, maybe what we have to do instead is break off the numeric portion so that we only use the number for sorting and then tack back on the letters. Pam, just a second here. So okay Pam, I’ve turned your microphone back on.

Pam: Yes. That’s where it becomes complex because call numbers sometimes start with J indicating they’re in the Juvenile section. Sometimes they start with F indicating they’re fiction. Sometimes, they’re a number because they’re non-fiction. It is kind of…

Rick: Well, so then we unfortunately are faced with the problem of… you know, this working in sort of a gross way but not in a perfect way, right? The chances are that… let’s just go and look at a different example of this. See if we can find one with a bunch more…okay so in this case, that’s 266 and this one is 266 and this one is 362 and so this one should be 270. Okay, so it didn’t work like that. It’s really not 259… it’s not working in a gross way either. Because you know, it just takes the first… you know, it takes this first and then…actually, it looks like there’s a space there so then it would take that second and then this 3rd and sorts it like that which means that you know, this is going to have an irregular relationship to a number. Now, it shouldn’t have a problem sorting alphabetically. I mean, it will… that is going to sort it alphabetically when it’s alphabetical. So if we look at these… let’s just see. Lewis Trilogy 3 is the first one…

Pam: Yeah…

Rick: Trilogy 1… let’s reverse the order and let’s just see if that changes it. I mean you know, it could be that I just haven’t… I still have it right. I have to be wrong more than once in a day but order… let’s see… asc. So we’re going to order ascending here and we’ll save that again and upload it because it appears to be ordering at descending at the moment. Refresh this, go back down to fiction. Yeah, actually that works. So F. Lewis Trilogy 1 is before F. Lewis Trilogy 2 which is before F. Lewis Trilogy 3. So to that extent, it works. It’s just not going to give you the result you are really looking for with the numeric values because there is 220.7. It really thinks that 220.7 Walton is a smaller number than 220.6 McDowell.

Pam: Oh it is. That’s correct.

Rick: No, 220.7 is a larger number than 220.6, right?

Pam: It’s .07.

Rick: 220.07. Oh yeah, oh great. Okay, it is working. Good, you know, I… perfect. I just… I did not read that 0. I’m so glad not to be wrong twice in a day. Oh my gosh. Okay, well…

0 Comments… add one

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
THIS IS A LIMITED TIME OFFER!
Subscription Options
0 comments… add one

Leave a Comment