Home › Forums › Front-end Issues › Can't get ACF fields after creating a new page
Hello, We are developing a crowd-funding platform based on ACF. In order to create a new funding-project we fill ACF-fields (in a HTML-form). On submitting we create a new WP-page:
$post = array(
'post_name' => $pname,
'post_title' => $pname,
'post_type' => 'page',
'post_parent' => 8,
'page_template' => 'project.php'
);
$this->post_id = wp_insert_post($post);
Strangely we cannot access the field variables with get_field(” … “) before we manually click onto “Save” or “Publish” on the page in the WordPress backend. Using wp_update_post() or wp_publish_post() make no difference.
To be more precise: we save a new page as draft and add several custom field entries to it (all done via php). when we then want to access those ACF-fields they are empty. when we go to the backend and choose our created page, the fields are visible and filled. ONLY when we click onto “Save” in the backend, we can access the fields via PHP get_field()
Do you have any suggestions?
Thank you very much!
Hi @julson
The issue here is that your custom code is not saving the reference field which links the ACF value to the ACF field.
Please use the update_field function to better save the custom fields:
http://www.advancedcustomfields.com/resources/functions/update_field/
Thanks
E
Hi @elliot,
thank you for your answer.
We use a OO-Form to create our new WP-page. After we create this page we use the following function to save the values:
public function updateFields($cover_pic_id) {
// update all custom fields in the projects page
update_field('project_name', $_POST[PNAME], $this->post_id);
update_field('short_description', $_POST[SHORTDESC], $this->post_id);
update_field('overview_text', $_POST[OVERVIEW], $this->post_id);
update_field('equity_text', $_POST[EQUITY], $this->post_id);
update_field('start_date', $_POST[STARTDATE], $this->post_id);
update_field('end_date', $_POST[ENDDATE], $this->post_id);
update_field('investment_goal', $_POST[INVGOAL], $this->post_id);
update_field('keywords', $_POST[KEYWORDS], $this->post_id);
update_field('cover_pic', $cover_pic_id, $this->post_id);
}
($this->post_id is a private member variable that saves the post-id of the page just created)
So all in short on submitting:
$this->createNewProject();
$this->updateFields($cover_pic_id); // update custom fields in project
maybe we do have to update the fields before we create the page? but that would not be logical?
As mentionned the values are visible in the backend, but we cannot access them with get_field() before saving the page manually.
Thanks Julson
Hi @elliot,
I just found the solution!
Using the field key instead of the field name on creating the fields worked!
We are happy 🙂
Thanks!
The topic ‘Can't get ACF fields after creating a new page’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.