I'm using ACF plugin and I want to pull a custom field from the previous page.

I have a WP site that has a homepage and then 20 sub pages. Each sub page is for a specific city and each city has its own contact phone number. Then interested viewers will go to an apply page.

So the flow will hopefully be HOME » Your city page » Apply page. Rather than make an Apply page for every city, I want to pull the custom field from the city page the viewer visited before arriving at the apply page.

I found this info: Get previously visited page ID

But it's a bit advanced for me. Anyone willing to dumb it down and show me what this might look like in practice?

The ACF plugin has the ability to pull a custom field from another page using IDs:

<p><?php the_field('city_phone', 123); ?></p>

That would return the custom field city_phone from post 123. How would I get the previous pages ID into 123?


1 Answer 1


get_page_by_path() should help you here. Something like this

$prev_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if ( $prev_url ) {
    $prev_path = str_replace( home_url(), '', $prev_url );
    $page = get_page_by_path( $prev_path );

That should give you the page object, where you can access it's ID like so - $page->ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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