I'm working on a plugin which registers a new post type, and I want to be able to define a default loop for the archive page. I've been trying to brainstorm the best way to do this, and I keep coming up short.

Basically, what I need to do is this:

My post type should display in archive pages as title, excerpt, and custom field only. I also need to run certain filters on the archive page.

I've been asking users to create their own archive-{myposttype}.php file, but that is a little over the heads of casual site owners.

So, thoughts I have had include:

  • Creating my own template file that I load on the template_redirect hook... probably out, as there's really no way to generate the correct markup for a given theme.
  • Trying to save a new file on plugin activation by some crazy regex processing of existing template files... I think that it might be possible, but I feel like I must be doing something wrong.
  • Asking users to choose an existing page to hold the archive for my custom post type, then using the_content filter to output my archive... which is very doable, but then I have to hack the query a good deal, and I end up breaking the is_query conditionals and so on...

This question may be too subjective to provide a good answer to, but I'd like to hear any thoughts on it.

1 Answer 1


In case anyone else comes upon this question, I'm posting the solution I ended up going with for my "Recommended Links" plugin. I think it solves most of the problems I that I anticipated pretty well.

  1. For users who want to completely customize the output, give the option of using the default WP archive template.

    This is the best solution because it fully leverages all of WordPress query features and conditionals, although its way too intensive for the average user to have to create their own archive-reclinks.php template and populate it with the correct logic and markup. I expect probably less than 5% of users will choose this option, but its there for those who want to.

  2. For ease of setup, allow users to choose an existing page to hold the archive.

    I borrowed the style from this from the Settings > Page for Posts dialog and stored the page selected in an option. The I just filtered the_content for that page with my own archive.

  3. For overridable archive styling, use locate_template() to allow users to override the plugins markup from their theme directory.

    This was the most elegant solution I could come up with. I included a mock template file in my plugin directory called loop-reclinks.php and included it in my filter on the content of the user-defined archive page using locate_template() like this:

    $old_query = $wp_query;
    $wp_query = new WP_Query( array(
        //  ... my archive query settings here
    ) );
    if ( '' === locate_template( 'loop-reclinks.php', true, false ) )
        include( 'loop-reclinks.php' );
    $links_archive = ob_get_clean();
    $wp_query = $old_query;
    return $content . $links_archive;

    (truncated for clarity)

    So, the end result is that overriding the loop template I defined is as easy as copying my loop-reclinks.php to the current theme directory and modifying it; while all the business and query logic is still handled by the plugin.

Hope this helps someone else. I'm still curious as the best way other have found for handling this, so I'd like to hear any feedback, or examples of other plugins that have gotten this "righter".

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.