If I use the simple code below:

function my_custom_rewrite_rules() {
add_action('init', 'my_custom_rewrite_rules');

I would expect to be able to visit http://example.com/rewriteme and see the content of the Hello World post while keeping /rewriteme in the address bar. It doesn't work, though. It actually redirects to http://example.com/hello-world/.

How do I get it to display the content of the Hello World post without actually changing the URL in the address bar?

(And yes, I've flushed rewrite rules.)

  • 1
    Have you tried index.php?p=1' because "Hello World" post is a post not a page. Not sure if that helps, because the rewrite rule seems fine. – gmazzap Jul 10 '18 at 18:09
  • 1
    your rewrite rule might actually be working assuming you flushed permalinks, the problem here is one of canonical redirect. Try changing it to index.php?s=hello instead to test if this is the case, you should expect to see a search page for hello if it works, confirming it's the canonical redirect and not a different problem – Tom J Nowell Jul 10 '18 at 18:32
  • @gmazzap I could have sworn I'd tried that already, because I had the same thought...but after just now trying it, it worked! In my actual case, I wanted to access a CPT, and it turns out what I needed was index.php?post_type=my_cpt&p=127 (just p=127 didn't work with a CPT). – Pete Jul 10 '18 at 20:04

If you just want to see the content of the /hello-world post on /rewriteme page you can try to add something like this on the page.php of your theme:

    if (is_page('rewriteme')) {
        // query for the about page
        $your_query = new WP_Query( 'postname=hello-world' );
        // "loop" through query (even though it's just one page) 
        while ( $your_query->have_posts() ) : $your_query->the_post();
        // reset post data (important!)

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.