1

I have a few custom post types on my website. One of these is a front page slider. I add some text, images, links and as soon as I publish the CPT appears as a slide and I'm a happy camper. The problem is when I need to preview the slide before it is live. I can preview that specific CPT post via the post preview button, but obviously I see the actual slide and not how the slide looks in that slider or gallery or archive.

I'm looking for a solution to preview the Drafts in an archive. Any help will be much appreciated.

1 Answer 1

4

The get_posts method of WP_Query is what does the heavy lifting as far as getting the posts to display. Before it does anything, however, there's a hook called pre_get_posts that you can hook into. The hooked function will receive a reference (pointer) to the current query object. So you can change the query vars to be whatever you'd like.

So...

<?php
add_action( 'pre_get_posts', 'wpse33020_pre_get_posts' );
function wpse33020_pre_get_posts( $query_obj )
{
    // get out of here if this is the admin area
    if( is_admin() ) return;

    // if this isn't an admin, bail
    if( ! current_user_can( 'manage_options' ) ) return;

    // if this isn't your slide post type, bail
    if( ! isset( $query_obj->query_vars['post_type'] ) || 'slider' != $query_obj->query_vars['post_type'] ) return;

    // change our query object to include any post status
    $query_obj->query_vars['post_status'] = 'any';
}

You'll probably have to change your post_type from slider to whatever you slider CPT is named.

As a plugin: https://gist.github.com/1343219

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.