1

I have a page, let's called it "Artists", that displays a list of posts in a custom post type, again "artists".

How can I redirect pepole who visit the Artists page to the first post in the "artists" post type?

1

This is assuming that your page 'Artists' has an ID of 10, so change that as necessary.

You can also amend the $args array as you wish to better suit your needs, if required. Here is the Codex for WP_Query, which shows that parameters that you can use.

Finally, this needs to go right at the top of your page (before any output), otherwise you'll get an error about header information having already been output.

if(is_page(10)) :

    $args = array(
        'posts_per_page' => 1,
        'post_type' => 'artists'
    );

    /** Get the posts that match and grab the URL of the first post */
    $posts = get_posts($args);
    $redirect_url = get_permalink($posts[0]->ID);

    /** Redirect to the specified URL */
    wp_redirect($redirect_url);
    exit;

endif;
  • This seems to just try and redirect the page back on itself... I echoed the $redirect_url and it echoed the current page permalink and not the post type – Dean Elliott Mar 20 '13 at 13:38
  • That means that there is no ID being passed by get_permalink(). I have spotted a small typo on my part and amended the example above, so please give it another try. – David Gard Mar 20 '13 at 13:43
  • I spotted the missing , myself and that had no effect. The query itself is outputting the permalink of the current page and not of the single post type – Dean Elliott Mar 20 '13 at 13:46
  • Try echo '<pre>'; print_r($posts); echo '</pre>'; to see what the query is actually grabbing. My guess would be nothing, hence get_permalink() returning the URL of the page you are on. That being the case, is the name of your Post Type correct, or have I mis-typed that? – David Gard Mar 20 '13 at 14:14
1

If you want to redirect a specific Page to the first post I suggest looking into /u/gmazzap's fantastic answer regarding assigning Page Templates as Post Type Archives.

If you want to redirect the Post Type Archive to the first post instead, you could use the below:

/**
 * Redirect post type archive page to first post
 *
 * @return void
 */
function wpse91590() {

    if( is_post_type_archive( 'artists' ) ) {

        // Grab the first artist post ID and store it in an array
        $posts = get_posts( array(
            'post_type'         => 'artists',
            'posts_per_page'    => 1,
            'fields'            => 'ids',
            'orderby'           => array( 'menu_order' => 'ASC', 'post_date' => 'DESC' ),
        ) );

        // If our array isn't empty, redirect
        if( ! empty( $posts ) ) {
            wp_redirect( get_permalink( $posts[0] ), 301 );     // Variable $posts[0] holds the artist post ID
            exit();
        }

    }

}
add_action( 'template_redirect', 'wpse91590' );

The idea is that we don't need the entire Post Object so we just grab the ID and redirect to it. Finally, we exit out of the script to avoid any further execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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