Wordpress 101: Filtering and Pretty URLS in Wordpress
Wordpress has many powerful built in features, but sometimes there are so many built-in functions that it’s easy to miss them. Adding rewrite rules and modifying the main loop query are sometimes overlooked, but they are very useful tools.
We recently had to create a posts page in Wordpress with a double taxonomy filter. In other words our blog home page had two drop downs to narrow down the content.
There’s a nice plugin for filtering custom post types, but the plugin doesn’t work with standard post types.
In our case, a Topic is actually a native Wordpress Category, and Industry is a custom Taxonomy. So, the url structure we’re going for is /blog/market/:market/category/:category/page/:page. Adding the page number at the end is easier due to some automatic redirects that Wordpress will do if you try to move it to the beginning of the url.
Permalinks
Since it helps SEO, the first step is to setup our pretty url structure. This can be done using Wordpress rewrite rules via the add_rewrite_rule method. A rewrite rule takes as arguments a regex that matches a pretty url, the “regular” urls it is matched to, and the order the rule is placed relative to the other rules.
One of the reasons some people shy away from rewrite rules, is that it can be difficult to understand how they work and which ones are being applied to a particular page. A great Wordpress plugin is the Wordpress Debug Bar. This will list the rewrite rule that is being applied and the query parameters used in a Request Object. The WP Debug Bar also has several companion plugins, including the Rewrite Rules plugin. This plugin will list all the rewrite rules. It makes debugging and implementing rules much easier.
Rewrite rules are cached in Wordpress, so it is important to continuously flush the rewrite rules with flush_rewrite_rules() as you work on implementing a set of rules. Once you are done, don’t forget to remove the flush call. When you change the permalink structure of your site, WP flushes the rewrite rules. This is why a common — often magical seeming — remedy for 404s on pages where you expect content is to change the permalink structure away from and then back to what it is.
Now that we have the correct routing with query variables populating, we have to update the core loop query to take into account our filter:
This is better than using a secondary loop, since it “just works,” in terms of working with The Loop. Modify the loop query can be done before the query is run using pre_get_posts.
In the snippet above it’s very important to set the conditional for modifying the query correctly. pre_get_posts is several times per page. For example it is run when getting a menu widget. This is why $query->is_main_query()
is needed.
Paging
Now that we have the correct items showing on the page, it’s time to add pagination. Fortunately Wordpress comes with built in methods to handle this.
paginate_links will give you the html needed to rendering the paging. You can include an ellipsis, and the current page and max pages is taken into account. The base lets you setup the structure of the url. Just put, “%_%” in the spot where the paging will be ( e.g. /blog/category/fun/topic/games%_% ). The format is what you’d like the paging to look like. %#% is where the paging part of the url goes. In our case it’s /page/%#%.
The above returns html that looks like the following. You can customize the classes, etc. of the output.
Next and previous page links can be easily added by using the built in WordPress paging controls (see next_posts_link() and previous_posts_link() for more info).
This is just one example of how Wordpress core functions can make your life easier. Whenever you try to do something with Wordpress make sure you’re not reinventing the wheel. Wordpress core functions and plugins will often help you out with what you’re trying to do.