Rick: So Pam, I’m going to turn your microphone on here because I want talk to you about sorting posts by call number.
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…
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…