I am developing a plugin and I want to show my custom post type (projects) next to the standard post type posts. I know I can do this by adding some code to the themes functions.php. But is it possible to achieve this within my plugin? The plugin should be usable from the start without requiring the user to do some extra steps.


2 Answers 2


Something like this should achieve your goal. You may need to remove the is_main_query() check depending on where you want this to show up though.

add_filter('pre_get_posts', 'projects_are_posts');
function projects_are_posts($query) {
    if (is_admin() || !is_main_query() ) {
        return $query;
    $types = $query->get('post_type');
    if (!is_array($types)) {
        $types = array($types);
    if (in_array('post', $types) && !in_array('projects', $types)) {
        array_push($types, 'projects');
        $query->set('post_type', $types);

  • Doesn't work for me. Somehow there is no post post-type in the $types array after visiting the homepage (but there is a projects post-type after visiting the projects-page)
    – styx
    Commented Mar 27, 2019 at 18:23
  • dump out $types on the home page and see what is in there. Have you tried removing || !is_main_query() from the short circuit return?
    – mrben522
    Commented Mar 27, 2019 at 18:29
  • I did both before. After entering the start page, the following entries are in the types array: nav_menu_item and page and 2 empty strings
    – styx
    Commented Mar 29, 2019 at 15:36

The only way to do this (show CPTs every time Posts are shown) reliably across themes would be to register a new template with your plugin, and have that take over the homepage completely. This would affect styling and functionality pretty dramatically - you could certainly include the normal header and footer, but then anything in between would just be your custom code and wouldn't match the rest of the theme. It would be similar to what WooCommerce does - they create their own templates. If the end user is PHP savvy they can copy that template into the theme and customize, but most users who just want a plugin to add a custom query aren't necessarily PHP savvy.

Themes are built in such diverse ways - from the PHP code itself to the CSS that can be dependent on a certain number of items - so unless you completely take over a template, you wouldn't have any guarantee of always safely adding the CPT query wherever posts are displayed. However, you could also set up your plugin to have an options page that covers the standard templates, and for example, have options for homepage, single, categories, archives, Pages, etc., and then in your plugin, only add the custom CPT query when it is that type of query and the option is on. So for example, for your homepage question, set up the condition as if(is_home && get_option('home_cpt') == 1), and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.