Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing ACF fields to WP API without clicking publish button #49

Open
mantismamita opened this issue May 14, 2016 · 4 comments
Open

Writing ACF fields to WP API without clicking publish button #49

mantismamita opened this issue May 14, 2016 · 4 comments

Comments

@mantismamita
Copy link

I’m having a problem displaying the ACF fields in the api from dynamically created posts using wp_insert_post()

Here is the function I'm using to import the data as a custom post type:

function mysite_import_json() {
  $json_feed = 'http://local.mysite.com/wp-content/test.json';
  $json      = file_get_contents( $json_feed );
  $objs      = json_decode( $json, true );
  $wp_error  = true;
  $post_id   = - 1;

foreach ( $objs as $obj ) {
    $title   = $obj['title'];
    $meta1  = $obj['taxonomy'][0];
    $meta2     = $obj['nom'];
    $meta3  = $obj['prenom'];
    $d       = new DateTime();
    $d->setTimestamp( $obj['created'] );
    $date_created = $d->format( 'Y-m-d H:i:s' );
    $post_meta    = array(
        'meta_1'        => $meta1,
        'meta_2'        => $meta2,
        'meta_3'        => $meta3,
    );

    $post_data = array(
        'post_title'  => $title,
        'post_date'   => $date_created,
        'post_status' => 'publish',
        'post_type'   => 'cpt',
        'meta_input'  => $post_meta,
    );

    if ( null === get_page_by_title( $title, 'object', 'message' ) ) {
        $post_id = wp_insert_post( $post_data, $wp_error );
        foreach ( $post_meta as $key => $value ) {
               update_field( $key, $value, $post_id );
        }
    } else {
        $post_id = - 2;
    }
  }
}

add_action( 'after_setup_theme', 'mysite_import_json' );

I’m guessing I need to make the WP API writable to the ACF fields. They do display once I click the Publish button though but there are too many posts to do this.

@mitch-rickman
Copy link

I'm running into the same issue with a CSV importing script I am working on. Has anyone found any solutions?

@chrishutchinson
Copy link
Contributor

Hey @mantismamita, can you post a sample of your JSON? I'll run this myself and see if I can get a solution. I suspect it'll just be a case of linking up the correct hooks

@mantismamita
Copy link
Author

Hey @chrishutchinson I did get it working but I've since moved on to other projects and no longer have access to the code. @mitch-rickman if memory serves correctly I think I did an update_post_meta() instead of update_field()

@mitch-rickman
Copy link

Thanks for the responses, @chrishutchinson & @mantismamita, I found a similar solve to as the one above. I was importing custom ACF fields, and the plugin I was using used update_post_meta(), which added the entries to the database, but it didn't link the value to the field. Using update_field() solved the issue.

I found this article which helped me come to this conclusion: https://support.advancedcustomfields.com/forums/topic/cant-get-acf-fields-after-creating-a-new-page/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants