WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 0 down vote accepted

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 */

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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