Parent Post From Another Post Type And A New URL Structure In WordPress

Hello, beautiful readers! In this post we are going to solve a very specific need for custom post types in WordPress. I’ve actually written about it two years ago, which is to select post parent from another post type. But today you’ll learn more deeply on the same topic with a live example.

Say there’s a project for building online courses website, we plan to have two custom post types in WordPress, which are “Courses” and “Lessons”. Registering two CPTs won’t be a big deal for us but the not-so-easy part is we’d like to set the URL structure of the lessons in such format:

http://my-online-course.com/lesson/[course-name]/[lesson-name]/

Taking my AngularJS series for example, it would look like:

http://my-online-course.com/lesson/angularjs-wp-api/lesson-1-using-angularjs-in-wp-theme/

So the workflow to manage our online course would be like, we always add a new course first, input some course information, prerequisites, curriculum etc. After that we’ll create as many lessons as needed, and for each lesson, we have to set the “parent post” for it, which is the course it’s attached to.

If you find this use case compelling, keep reading and here’s what I’m going to show you (preview the final demo if you’d like):

  1. Registering two custom post types: Courses and Lessons.
  2. Updating the “Parent” meta box so we can choose a “course” as the parent post for a “lesson”.
  3. Setting the exactly URL structure we want.
  4. Updating the permalink of the CPT to reflect the new URL structure.

Ready? Let’s go! Read More…

Getting Categories With Posts In A Single WP API Request

UPDATED March 03, 2016:

Post has been updated. Now we use "rest_prepare_category" filter for this job.

UPDATED March 02, 2016: 

With the latest WP API V2 Beta12, the filter "rest_prepare_term" I used in this post no longer work and it should be changed to "rest_prepare_category". I'll update the post content ASAP.

A while back ago I wrote a post to show you how to “Adding Fields To The JSON Response Of The WP REST API“. In the first scenario why we’ll ever need to add fields to WP API, is for people who want to build an accordion menu with categories as the first level links, which when being clicked, the posts would be collapsed as the second level links.

We know we could use WP API to get a single category info, and we can also get the posts for a certain category, but unfortunately we can’t do both in a single API request. Read More…

Display Post Excerpt To The More Tag With WP API V2

Ho Ho Ho! It’s December now so you get a Santa-style greeting from me! Today I’ll be showing you a simple trick that I got asked a while back ago in one of the comments, that is: “how can we display excerpt to the more tag (<!–more–>) with WP API V2”?

By default, WP API V2 has the excerpt field in the posts response, but the auto generated one is often not what we want.

So here are things we’d like to hack around the WP API and get the results we want:

  • Get the excerpt with read more link
  • Get the excerpt from text before the more tag
  • Display content of the shortcodes or oEmbed links in your excerpt

Now let’s get started. Read More…