How to add custom post types to loop

When the conditionals are right

By Josh Robbs
November 20, 2018
Filed under:
Skill level: Master
Task: Add custom post type to main WordPress loop
You need:
  • A self-hosted WordPress website
  • A safe place to store a function on your website. This can be in the functions.php file in a child theme or in a plugin.

Questions Answered

  1. How do you add custom post type to loop?

Adding custom post types to the loop can be a giant pain.

I mean, making them is easy. You install Pods, click a few buttons, name it, and click save. Ta-daa! A brand new CPT. Then you have to get them in the loop.

The goal for this script is to add a custom post type ("tutorial") to the main loop without messing up anything else.

The standard edit your code at your own risk disclaimer

Whenever you edit your code, you risk breaking breaking something. That is why should never work on your live website.

Steps you can take to protect yourself in case something goes wrong:

  1. Back up the site.
  2. Make copies of the files you're editing.
  3. Test your work on a development/staging site.
  4. Theme customization should be done to a child theme.
  5. Edit via external text editor. If you are editing via WordPress and WordPress goes down, you can't fix your error.
  6. Put custom code in your own plugin. Do not make edits to existing plugins.
Learn more about adding code to your website safely.

The code

function add_cpt_to_loop_and_feed($query) {
    //---- Don't run in admin area
    if(!is_admin()) {
        // Add custom post types
        if ($query->is_feed() || ($query->is_main_query() && $query->is_front_page())) {
            $query->set('post_type', array('post', 'tutorial'));
        }

        // Return query
        return $query;
    }
}

add_action('pre_get_posts', 'add_cpt_to_loop_and_feed');

I originally found this snippet at Inspirational Pixels. I would have just linked to it, but they don't explain how to tweak the code.

Making the code your own

Once again, we're using conditionals with a custom query.

Line 3 asks if the query is NOT for an admin page.

Line 5 is a little fancy. It asks if this query is for a feed or (||) is it both the main loop and (&&) the front page. The last 2 conditionals are evaluated together because they are in parenthesizes.

Line 6 sets the post types for the query. Notice that it isn't adding. If you don't include posts, posts will not show on the front page. In this example, I wanted to keep posts and add tutorials. I could have added any number of post types.op_and_feed');

Line 14 connects the function "add_cpt_to_loop_and_feed" to the event "pre_get_posts". (Technically "pre_get_posts" is a hook. I've never understood that terminology. Just think of it as an event and that you've added a function that will run with that event occurs.)

Useful?

Useful? Wow, that's tragic. That might be the worst newsletter pitch headline I've ever written.

As unsexy as it may be, my goal for this site is to be simple and useful. If you'd like to know when I add a new article, get on the list.

It's an automated newsletter that will notify you when I make a new post - once a week at most. The closest you'll get to a sales pitch is when I announce webinars.

Josh started WP Master Builder to help small businesses get more done on their website in less time. He’s also a marketer and designer. There’s always a chance that he’s watching “Sky Captain and the World of Tomorrow” again or helping his wife build pipe lamps.

Submit a Comment

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Learn something?

Share it so others can use it too!