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

  • [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-no-posts-found][wpml-string context="wpv-views"][/wpml-string][/wpv-no-posts-found]

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.