Created
August 26, 2021 17:01
-
-
Save taotiwordpress/31fb53433a34f20009d242338745f9f8 to your computer and use it in GitHub Desktop.
Programmatically Generate Post with ACF content #acf #wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Create Post Example | |
* | |
*. Note: See https://www.advancedcustomfields.com/resources/update_field/ for notes. | |
*/ | |
// Create new post. | |
$post_data = array( | |
'post_title' => 'My post', | |
'post_type' => 'post', | |
'post_status' => 'publish' | |
); | |
$post_id = wp_insert_post( $post_data ); | |
// Save a basic text value. | |
$field_key = "field_123456"; | |
$value = "some new string"; | |
update_field( $field_key, $value, $post_id ); | |
// Save a checkbox or select value. | |
$field_key = "field_1234567"; | |
$value = array("red", "blue", "yellow"); | |
update_field( $field_key, $value, $post_id ); | |
// Save a repeater field value. | |
$field_key = "field_12345678"; | |
$value = array( | |
array( | |
"sub_field_1" => "Foo", | |
"sub_field_2" => "Bar" | |
) | |
); | |
update_field( $field_key, $value, $post_id ); | |
// Save a flexible content field value. | |
$field_key = "field_123456789"; | |
$value = array( | |
array( "sub_field_1" => "Foo1", "sub_field_2" => "Bar1", "acf_fc_layout" => "layout_1_name" ), | |
array( "sub_field_x" => "Foo2", "sub_field_y" => "Bar2", "acf_fc_layout" => "layout_2_name" ) | |
); | |
update_field( $field_key, $value, $post_id ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment