This is the 54th in a series of live lessons on using WordPress and Thesis as a Content Management System (CMS). In this lesson we customize the query string for archive pages so that the results are sorted by meta value. In this case – by callnumber. We capture the global variable $query_string, deconstruct it into its various parts and then add new elements to it.
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
The next thing we’re going to look at is how to sort archive results by call number. Now you know that last week, we sorted our custom catalog by call number. So you know, so this is returning the results sorted by call number and that was, as you may recall, sorting it by the meta tag and the meta tag is call number. And that worked here but what Pam wants is to be able to click on one of these things which pull up an archive page of all of the adult fiction books. And she would like these to be sorted by call number so if we… let’s look at non-fiction. It’s going to be easier to see the lack of organization…lack of call number organization, I think.
Okay so if we look at this one, that call number is 0106PET and if we look at this one, this call number is 1583SMITH. And you know what? I’ve already fixed that so it’s already working right. So I’m just going to walk through the process of fixing that and the process of fixing that is to create a new function. And this function sorts the archive pages by call number. If you recall what we did last week, up here what we did was we added a meta key to the arguments in our query. So we added meta key, we ordered by meta value and we ordered ascending. And so that argument goes into our query and then the query brings up these things in that order. Well, we’re doing the same thing down here in our archive pages.
So we create a function called order archive and we add that function to the Thesis hook before content and then the function of course, is byob order archive. And what we are doing is we are deconstructing the query string. So first, we grab the query string by taking advantage of global query string and then we break the query string up into its various parts using this parse string. This takes query string array and breaks it up into individual query elements and each individual element goes into this args array. And then we are going to add new array items to this query string.
Now the query string is essentially the query that calls the specific page. And so, up here in our WP query you know, up here in WP query, we created this query string which was essentially these arguments. We want at post type of catalog items. We want the taxonomy location and we want you know, unlimited posts. We want to sort by these meta values. That was our query string here. If you look at our menu, this up here is our query string. Audience equals adult and location equals non-fiction. It’s this section right here, right behind the question mark.
Now if we… so you might think that the way to do this is just to… instead of grabbing the query string, breaking it up and adding stuff to it, you might think we could just say you know, args equals array and then meta key equals order number… order by equals meta value, order equals ascending and then say query post args, the problem with that is that it would… if we did it that way, it would completely erase all of the existing query data in the query string and we don’t want to remove that existing query data. What we want to do is we want to add to that query. And so, the way we add to the query is this way. Grab the query string, take the string and break it up into its component parts, those parts go into an argument’s array and then we build a new argument. We add on to the argument’s array with meta key equals call number, order by equals meta value, order equals ascending.
Now this stuff here is… you haven’t seen this syntax before. Because WordPress, you know, the stuff that we do in WordPress doesn’t require it very often. But this is syntax for an associate of an array. And the way the associative array works is that you have a… an associative array is a collection of name value pairs and this is… the name is meta key and the value is call number. The name is order by, the value is meta value. The name is order, the value is ascending. So those are name value pairs and the way to identify a specific element within an array is to enclose them in brackets like this. And so, what we’re saying here is inside the arguments, we want to grab the meta key name inside of this arguments array and we’re going to change its value to call number. Grab the order by argument inside of this and change its name to meta value.
Pam asks, “What is meta value?” Our meta value was the… in this case was call number, remember? We created that custom post meta called call number and then we got post meta in order to get a hold of the call number. So that’s what our meta value is, it’s the call number and the value inside the call number. So call number is the name and the value inside of call number is the value in that name value pair.
And then we just… now we have completely recreated the query string including all of the old stuff plus our new stuff and now we can replace the old one with this query post args because we kept all of the old stuff. So that’s how this little loop here worked and if you go look at you know, how it works here…actually, we can see this really well I think in fiction. If we go to fiction and you select The Silver Sword… oh yeah, call number. No, you can’t see it. Well, let’s do it in non-fiction, I’m sorry. When do we get here to non-fiction? Okay, so we can take a look at this, say take and read and our call number’s 016PET and then we come over here to the next one. Our call number’s 1583 so 016 is smaller than 158 and so, this is the 2nd one and then Indelible Ink is 16. 16 is greater than 15 so it shows up next and so on and so forth. 230 is bigger than 16 and so, that comes next. So that is the order by.
Pam says, “No, no, no. 16 comes before 158.” Yeah, but remember our conversation from last week in the whole order by thing. The WordPress does order by in alphanumeric way and so, it compares… it doesn’t compare the value of the number. It compares the specific numeral to the specific numeral on the next piece as if it were a letter. And so, now she doesn’t think we can really order by call number like libraries do. Well you know, maybe there’s a way of doing something that… Brian suggested to me earlier today and that is adding the leading 0 to it, right? So rather than 16, it could be 016. I don’t know if that does violence to the call number system or not but you could add the leading 0 to it. In fact, if you really wanted to, you could just add the leading 0 into the database and then extract the 0… remove the 0 from when you print it which is another thing we could do too, if you want to do that. So it’s something for us to consider for next time.
So anyway, we do have a system now that sorts everything. Whether it’s recent items for adults or it’s using our library…I mean, our various… using our various menus. It still sorts anything.
Okay so now, we come to our 3rd and final thing. If my throat holds out, it’s given me a little bit of problems here. Pam says that that would work because numbers below 100 are quite uncommon. So there you go. We could actually put leading zeros in to make it work so that’s something to consider.