0

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?

| improve this question | | | | |
2

get_page_by_path() should help you here. Something like this

<php
$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.

| improve this answer | | | | |

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.