yevg
yevg

Reputation: 1966

Grab the content for a given request programmatically in Wordpress

Is it possible to grab the content that a Wordpress site would return for a particular request programmatically within the code.

In pseudo-code:

my-wp-template.php

request = get_the_request() // www.example.com/foo/bar

content = get_the_content(request) // the content returned if I were to go to www.example.com/foo/bar in the browser

Basically, its like a curl request but using Wordpress internals.

Why would you want to do this?

I am routing a request in the form example.com/foo/bar to my-wp-template.php.

I would like that template to function such that it returns the content for exmaple.com/bar, as if the request was made with no /foo/ slug.

A redirect would not work in my use-case.

Upvotes: 0

Views: 37

Answers (1)

Dirk J. Faber
Dirk J. Faber

Reputation: 4701

This can be done by first retrieving the post ID from the route you want to with url_to_postid(), and then use that ID to get the content of this post/page using get_the_content(). This is how:

$content_of_foo_bar = get_the_content(null, false, url_to_postid( 'www.example.com/foo/bar' ));

Upvotes: 1

Related Questions