Sorting a Custom Post Type Archive by a Parent’s Field Using WP Types & Views

If you’re using WP-Types (aff) to build your WordPress website with custom post fields and custom post types, you may find the occasion where you’d like to build a custom post type archive sorted by a custom field associated with the archive’s parent custom post type.

Say that ten times fast.

You can’t do it. I promise. (You might be able to say it, but you cannot natively perform this function.)

The only thing you can do with a WordPress archive is to display the WordPress archive.

Screen Shot 2013-08-18 at 2.37.03 AM

See? No options. Bummer. Don’t lose hope. Here’s a brief tutorial on what you can do instead to achieve the end result using WP-Types.

First, you’ll need to create a Views Template. In my example I’m sorting a list of coupons by the name of the item contained in the “coupons” custom post type parent custom post type, “Directory Listings”. Exciting.

I created a Views Template called “Master Coupon List.” The only thing this Views Template contains is a call to a View.

Screen Shot 2013-08-18 at 2.42.24 AM

 

The “All the coupons” view Is quite simple. It queries all the posts in the  Directory Listings post type (that’s the parent post to coupons), and sorts them via the custom field that I wanted to use to sort (which is the name of a product). You don’t need to write any code, just select the correct options. Add another filter to select related posts that are a child to the Post set by parent View.

Screen Shot 2013-08-18 at 2.44.05 AM

And here’s what’s in my Meta HTML for that View

[wpv-layout-start]
[wpv-posts-found]
  • [types field=”box-name” id=”$subscription-box”][/types]: [types field=”coupon-information”][/types] [wpv-if code=”wpcf-has-code” evaluate=”$code=’1′”] Use code [types field=”coupon-code”][/types].[/wpv-if] [wpv-if exp=”wpcf-has-expiration” evaluate=”$exp = ‘1’”] Expires [types field=”expiration-date-new” style=”text” format=”F j, Y”][/types]. [/wpv-if]

 


[/wpv-posts-found]
[wpv-no-posts-found][wpml-string context="wpv-views"][/wpml-string][/wpv-no-posts-found]
[wpv-layout-end]

Finally, you have to display your Views template in your archive. To do this, copy your archive file (in the theme I was using it was named index.php). Duplicate it and rename it to archive-{POSTSLUG}.php. Replace {POSTSLUG} with the post slug of your custom post type. In this case the post slug for my CPT was coupons, so I renamed my file archive-coupons.php. Place it in your theme directory, yank out all the code for the loop, and replace it with your (formatted) display code. In this case my View “coupons for one box” was formatted the way I desired, so all I had to do was drop in the PHP to display the View template. This is terribly uncomplicated.
echo render_view_template('145');
Replace 145 with the post id of your view template. You’ll find it in the URL of your editing screen. Although I prefer to use Views instead of coding fiddly PHP, it’s oftentimes easier to drop in a view or view template right into your theme templates. I use the render_view_template code to drop in custom post-information headers based on custom taxonomies. It’s a snippet I use over and over again and don’t feel the need to recode it in the 5 places it appears in my WordPress theme every time I decide to change a slug name.

If you only need to alphabetize your post title, that’s easy. You’ll create a custom archive-{POSTSLUG}.php. You can then change your query to order by a variety of parameters defined by WordPress. You can order by a custom field that’s in the post type archive query by using the meta_value parameter.

Let’s Talk About Subscription Boxes

This is sorta funny, because my primary blog is all about subscription boxes. But I don’t mean the kind of subscription boxes you get in the mail with the latest beauty products. Or for you guys out there, the kind with fishing lures or whatever guys like to get in the mail.

Nope, the kind that get you subscribers. The kind of subscription boxes you care about for your blog, no matter what kind of blog it is. You have an email list, right? If not, get one now. Niche site gurus tend to recommend Aweber, I prefer the sleek design of Mailchimp (that’s my referral link and I heartily recommend it!). Forget about using Feedburner’s mailing list. Given the recent sunsetting of Google Reader, you don’t really want Feedburner controlling your list. I do use Feedburner for serving up my RSS feeds on my main site, but I prefer the control I get over my mailing list. Truth be told, I’m still amazed at how few people really use RSS and prefer to surf, get a newsletter, or get fed new posts through Facebook or Google+ (join me on G+!).

Any of these platforms could die, but you’ll still have your mailing list. Your site could go down, get hit with a Google penalty, and you’ll still have your mailing list.  Do you get the picture? Get a mailing list.

Fill your mailing list with a Subscription Box

Now, in order to keep that mailing list nice & healthy, you’ll need subscribers. Along with all of your other efforts, you’re going to want some way to entice readers to join up. Whether it’s the promise of the latest news, a free special report, or some other exclusive content, you’ll probably want to offer it in exchange for your email address.

And how do you get those email addresses? You don’t need any fancy coding and in fact, I have found the easiest to use advanced WordPress plugin for collecting addresses from visitors: Hybrid Connect. I am a Hybrid Connect affiliate and this is my personal recommendation.

The Hybrid Connect plugin is amazing. Now, you might say, Brandy, your subscription boxes are, well, kinda ugly. That’s because I’m testing them. Hybrid Connect does automatic split testing with variations that you choose. Set up as many variations as you want, start your test, and Hybrid Connect will determine which subscription box works the best. This is a new site, so I’m running tons of tests to see which settings work the best. When I find one that performs well, I can run new tests.

And I don’t even have to show you how to use Hybrid Connect. One of the best parts? There are really short videos on nearly every page walking you through the basics so you can get up & running in a snap. Need something special? On Hello Subscription (my website about physical subscription boxes) I use a Pinterest plugin to make a Pinterest button appear whenever a user hovers over the photos. I didn’t want to touch the HC code, so I asked them about it, and they went into my back-end and fixed it. Amazing service for a relatively cheap plugin.

It takes only a few minutes to set up, and has awesome features like responsive design, Facebook connect (so if a user is logged into Facebook, she can just press a button and get signed up to your list). And tons more. Because I use Hybrid Connect on multiple sites, I selected the $67 version (I didn’t need the $149 license because I don’t flip websites or develop for clients). It was relatively cheap, and having automated newsletter subscriber capture is golden. If you want to see something a little flashier than what’s up here (remember, content always before looks! as long as your blog doesn’t look like garbage, anyways), check out the subscription boxes here.

What do you think?