Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have two pages on my Wordpress site:

mysite.com/?p=100
mysite.com/?p=200

I think I should be able to hook into Wordpress' actions somewhere and re-query the post, so that content for mysite.com/?p=200 would be displayed on the page mysite.com/?p=100

Based on feedback from the first answer, I think it would need to look something like this:

add_action( 'pre_get_posts', 'wpse35832_pre_get_posts' );   

function wpse35832_pre_get_posts( $query ){

    $pid = '';

    if( ! empty( $query->query_vars['page_id'] ) )
    {
        $pid = $query->query_vars['page_id'];
    }
    elseif( ! empty( $query->queried_object->ID ))
    {
        $pid = $query->queried_object->ID;
    }

    if($pid == '100'){
        $query->query_vars['page_id'] = 200;
    }

    return $query;

}

And actually that function does work - but with one caveat: If I use pretty permalinks (mysite.com/name-of-page/), the title of the page is for the original post. However if permalinks are in the query variable style, everything seems to work fine.

What am I doing wrong, and how can I fix it? I tried unset($query->queried_object so that it would need to be re-queried, and doing that causes a redirect back to the original page.

Basically I'd like the whole webpage that is rendered to be exactly the same as it would be for the other post, including title tag, post title, and post content, and so I'm trying to hook in early enough to make that happen.

I realize I could create a custom page template, something like post-200.php and inside that use the query_posts function and display content. This is not what I'm trying to do though.

I admit this would serve no practical purpose on most blogs, but I think it is possible and I hope you guys can give me some help with doing it.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

pre_get_posts hook should work for this:

add_action( 'pre_get_posts', 'wpse35832_pre_get_posts' );   
function wpse35832_pre_get_posts( $wp ){
    if( $wp->query_vars['p'] == 200 ):
         $wp->query_vars['p'] = 100;
    endif;
    return $wp;
}

It may work a bit differently if pretty permalinks are used, different query vars may be set, not sure though.

share|improve this answer
    
Regardless of permalink settings, there should always be an identifyable factor inside the query to determine when your given post is due to be loaded, which should allow you to safely alter the query and grab something else, you have my +1.. :) –  t31os Dec 8 '11 at 13:16
    
@Milo - Thanks! This got me going in the right direction! I updated the question because I'm having a small problem with the page title when permalinks are on, but otherwise this is working really well. –  cwd Dec 8 '11 at 15:01

Your Answer

 
discard

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.