All posts by Dakota

WordPress Custom Taxonomy Archive for Custom Post Types

Share Button

Part of a series of posts on advanced WordPress customization, this article covers the implementation of a WordPress Custom Taxonomy archive for Custom Post Types.

Desired Effect

What we want in the end are URLs that look like example.com/post-type/taxonomy-name/taxonomy-term leading to a listing of Custom Post Type objects. It’s like example.com/blog/tag/tutorial, but with Custom Post Type and Custom Taxonomy instead of the built-in “post” post type and “tag” taxonomy. Posts should reside at example.com/post-type/%postname%/.

Approaches

There are a couple approaches to implementing this functionality in WordPress. I’ve outlined the two potential approaches below, and then explain my solution.

Custom Permastruct, Manual Custom Query

One could use a Custom Field on the Custom Post Type to store the taxonomy name and term in a key-value store. One could then create a new custom rewrite tag via add_rewrite_tag() to allow rewriting of taxonomy-name/taxonomy-term to a GET query variable. Finally, one could write a plugin and/or customize their theme to extract the newly-added query variable and use it to construct a custom SQL query or WP_Query() object. This would hijack The Loop causing it to only return appropriate posts.

Drawbacks, Other Notes: This approach relies on a Custom Field on each Custom Post with the proper name and value. This means if something is wrong there, the post won’t show up properly. There is no unified system to view all the values of that Custom Field across all posts, and no way to automatically rename them if the need arose. One could use the Advanced Custom Fields plugin to ameliorate some of these issues, but it’s not ideal. Furthermore, this system requires heavy customization of the Theme, which makes it much less portable. Not only with the custom query parsing and execution, but you must also write a custom generator for the taxonomy term links and such.

Hijack Existing Functionality in register_post_type() and register_taxonomy()

register_post_type() and register_taxonomy() already make calls to add_permastruct(), add_rewrite_tag(), and add_rewrite_rule(), so why can’t we hijack those to achieve the desired result? It turns out that you can, but it’s not as straightforward as you might expect. Details below.

Drawbacks, Other Notes: This approach utilizes the taxonomy system, and so benefits from all of the functionality and UI already written for it. Furthermore, it does not require such heavy customization of the theme, making it more portable. So far, the only drawback is that I haven’t figured out how to support multiple taxonomies.

Solution

It took a lot of digging, but I finally located most of the solution on the WordPress StackExchange. It relies on a little bit of under-documented functionality in register_post_type() and register_taxonomy() to hijack the behind-the-scenes calls to add_permastruct() and friends and achieve the desired outcome. I documented this solution on the WordPress support forums. The first step is to register the custom taxonomy with a few special modifications to the $args, particularly rewrite:

What this does is hijack the call to add_permastruct() on line 375 of taxonomy.php in the register_post_type() definition which WordPress generates the taxonomy archive page from. We also hijack add_rewrite_tag() on like 374 which creates the rewriting tag that we reference next. slug must be in the form <custom-post-type-name>/<custom-taxonomy-name> where <custom-post-type-name> is the value of the $post_type parameter when you call register_post_type() and <custom-taxonomy-name> is the value of the $taxonomy parameter in register_taxonomy(). This is the call that actually creates the custom taxonomy archive. Then we register our custom post type, with a few modifications to $args, particularly to rewrite and has_archive:

Since we’ve already tied the custom post type and custom taxonomy together in their initialization, we don’t have to call register_taxonomy_for_post_type(). Modifying $rewrite['slug'] allows us to hijack the call to add_rewrite_rule() on line 1309 of post.php with %example-taxonomy%, which is the name of the rewrite tag we created in the call to register_taxonomy() above; this rewrites example.com/example-custom-posts/example-taxonomy/term/post-name to example.com/example-custom-posts/post-name, though we could change that. Importantly, we must set has_archive to something other than true, this creates the regular custom post type archive index (see here). Last, we hijack add_permastruct() again to create the regular index for the custom post type. It took me a while to figure out exactly why and how this worked, and I hope that by documenting it here others may benefit.

“I don’t see Apple dramatically improving or replacing Objective C anytime soon.”

Share Button

Jason Brennan, in February of 2014:

I don’t see Apple dramatically improving or replacing Objective C anytime soon.

Five months later:

Apple introduced a boatload of new consumer features for OS X and iOS today, but one of the biggest announcements for developers could be its new programming language, Swift. Craig Federighi just announced it, saying that Apple is trying to build a language that doesn’t have the “baggage” of Objective-C, a programming language that came from NeXt that has formed the basis of OS X and eventually iOS.

I’ll have this in a bread bowl.

This new “Apple SIM” could legitimately disrupt the wireless industry

Share Button

Perhaps the most interesting news about Apple’s new iPad Air 2 tablet is buried at the bottom of one of its marketing pages: It will come pre-installed with a new “Apple SIM” card instead of one from a specific mobile operator.

If anything can disrupt the carrier marketplace, this is it.

It’s early, but it’s easy to see how this concept could significantly disrupt the mobile industry if Apple brings it to the iPhone. In many markets—especially the US—most mobile phones are distributed by operators and locked to those networks under multi-year contracts. People rarely switch operators, partially out of habit and satisfaction, but mostly because it’s annoying to do so.

Carrier lock-in is the bane of market competition. The ability to easily switch carriers and preserve your cellular identity, keeping not only your device but your existing number, without anything more than that device itself—this is the holy grail for carrier customers.

Via @theloop.

Excellent Sheep

Share Button

I recently had the opportunity to attend a talk by William Deresiewicz, author of the recently published Excellent Sheep: The Miseducation of the American Elite and the Way to a Meaningful Life. This piece was prompted by his comments on the direction and decisions of today’s young people as they approach their majority and contemplate higher education.

If excellence is the goal, “Education” is the obstacle.

To the capable and the driven, who today account for a great number of the sons and daughters of Middle Class America, an elite education is the stairway to success. It is, in fact, the essence of the Middle Class Dream. While the American Dream is the broad belief that success can be achieved through hard work and perseverance, the Middle Class Dream applies that belief to the following premise: if the most successful people come from elite universities, then the best path to success is through those universities. It is the belief that an elite education is the road down which hard work travels to become success. Unfortunately, the numbers back this belief: according to the  U.S. Department of Education’s National Center for Education Statistics, higher education correlates strongly with higher income. The Center writes, “In 2012, young adults with a bachelor’s degree earned more than twice as much as those without a high school credential…”

A pragmatic parent’s response is obvious: send your kids to college to insure their future. Send them to the best college they can get into, because the bigger the name on their résumé, the better their prospects are in the job market. College is an investment—insurance against economic uncertainty—and, as in finance, the bigger the initial investment, the larger the return. The goal is no longer learning, the goal is achievement and the appearance of educational excellence. Education has become a system and, like any system, the most successful are those within it who play the game to their advantage. As Deresiewicz writes in his essay “Ivy League Schools are Overrated”, elite education is structurally opposed to intellectual curiosity: “Students are regarded by the institution as “customers,” people to be pandered to instead of challenged. Professors are rewarded for research, so they want to spend as little time on their classes as they can. The profession’s whole incentive structure is biased against teaching, and the more prestigious the school, the stronger the bias is likely to be. The result is higher marks for shoddier work.” Students avoid risk like the plague, meeting the requirements of courses but without the freedom to explore the content or develop a true understanding of it. Deresiewicz continues, “Once, a student at Pomona told me that she’d love to have a chance to think about the things she’s studying, only she doesn’t have the time.” This extends down into the admissions process and from there into high schools. Getting into an elite college is an act of perseverance: forego free time, abandon excellence, and throw yourself bodily into the process of gaming the higher education system and filling every box on the admissions offices’ checklists. I know this because some of my friends did it: they played the education system and were admitted to Harvard, MIT, Stanford, Princeton, and Pomona (among others; whether or not they enrolled there is another story).

Society believes that education produces excellent individuals. Excellent individuals are capable, principled, and interesting. Elite education, then, does not produce excellent individuals. It produces, to use Deresiewicz’s book title, “Excellent Sheep.” As he writes in the book, “Our system of elite education manufactures young people who are smart and talented and driven, yes, but also anxious, timid, and lost, with little intellectual curiosity and a stunted sense of purpose: trapped in a bubble of privilege, heading meekly in the same direction, great at what they’re doing but with no idea why they’re doing it.” People like this would only ever be called “interesting” by a psychologist studying unbalanced personalities.

What, then, does it mean to be interesting? We talk about well-educated people being interesting and engaging. They say and do interesting things. One of my father’s college professors once told him, “The most interesting people know the most interesting things.” Of course the most interesting people have the most interesting knowledge. But how do they get it? How do they become interesting? How do they achieve this most sought-after personality trait? Was it something they read? Was it something they heard? Was it the college they went to? People search after these answers, but never find them. I think it’s because they can’t be found. There is no book you can read to become interesting, no lecture you can attend. There is no college that can bestow it upon you at graduation, for no school can collect books that don’t exist and offer lectures that can never be heard.

If there is one thing I have learned in my meager time on this earth, it is that interesting is not a status you can achieve. Interesting is not a badge you can earn, a box you can check off, an award you can receive, nor an a rank you can attain. Interesting is a state of being. It is a process, continuous; it is the very act of being interested. “The most interesting people know the most interesting things,” not because they learn them to become “interesting”, but because they study that which they find interesting. Being interesting is being intellectually curious, exactly what our elite universities prevent. And conveniently, the most interesting people (by this definition) are also some of the most successful in life. They are excellent individuals, not just excellent sheep.


If you’re on Medium, this story was also published there.

 

iCloud Photo Library

Share Button

Apple today announced iCloud Photo Library, allowing users to maintain a single photo library across all their devices with photos stored in their original format and resolution on iCloud:

Every photo, every edit, every album now lives in your iCloud Photo Library, easily viewable and consistent on all your iOS devices. Automatically.

Looks like Gruber was right on the money with his sources for iCloud Photo stuff seeing a major improvement in 2014.

It also looks like Peter Nixey has been vindicated, finally:

1. I want the canonical copy of my iPhoto library in the cloud. One iPhoto library in the cloud, many devices with access to it. I want to edit, organise and delete photos on any device and see the same changes on all other devices. No master/slave setup – just straight cloud access.

According to Craig Federighi, this is how it works. Unfortunately, it doesn’t seem like Apple read this next part:

2. You can charge me for this. I suggest $5/month. Maybe that’s a bit more than it costs you at the moment but that’s what I’m prepared to pay and we both know that you’ll do very well out of this in the long run. However for that I want unlimited space including for all of my videos. FYI that’s not what really I’m paying you for. I’m really paying you for the peace of mind that you’ve got my memories safe-guarded. I’m technically paying you for insurance. The utility this offers just the carrot that gets me over the hump of paying you.

Pricing matches iCloud pricing, and iCloud Photo Library takes up space in iCloud:

  • Free—Up to 5GB
  • $0.99/mo—Additional 20GB
  • $3.99/mo—Additional 200GB
  • Tiers up to 1TB

Personally, I think this whole pricing tier is a mistake. There are too many levels; it’s too complicated. Nobody should have to worry about keeping track of their data size, with the incumbent concerns of not exceeding their data cap. As Backblaze has shown, robust data storage is cheap enough to offer a single, unlimited price point. This is what Apple needs to do.

Compared to the overall price of the device and the price of service contracts for cellular coverage and data (for iPhones and iPads 3- and 4G), $5/mo is affordable. Apple doesn’t even have to operate at cost for this service, but Backblaze has shown that it can be done, for desktop customers who doubtless have many times the data storage requirements of iCloud users.

Apple: if you’re listening, do what Peter Nixey said and make paid iCloud Drive storage have two tiers. One free, up to something small like 5GB, and one paid, unlimited data for $5/mo.

Tim Cook On Apple Pay

Share Button

We believe Apple Pay is going to be huge.

Apple CEO Tim Cook, during the company’s September 2014 Special Event.

Mac App Store: The Subtle Exodus

Share Button

The Mac App Store can be better than this. It should be better than this.

Let me make it absolutely clear why I’m writing this. First and foremost, it’s because I deeply care about the Mac platform and its future, it pains me to see developers abandoning it. The Mac App Store can be so much better, it can sustain businesses and foster an ecosystem that values and rewards innovation and high quality software. But if you talk to developers behind the scenes or explore the Mac App Store, you’ll find something completely different.

Via Daring Fireball.