• Resolved slotigork

    (@slotigork)


    I am trying to add new posts based on a json file programmatically, with one of the fields being a file field. This is what an object from the json might look like:

    
    "title": "Visualisering av ?gande i startupf?retag",
        "forfattare_1": "Dana G",
        "forfattare_2": null,
        "forfattare_3": null,
        "pagaende_avslutad": "avslutad",
        "field_handledare_value": "Daniel L",
        "field_foretag_value": "Qoorp",
        "field_amnesgranskare_value": "Mats L",
        "field_estimated_value": "2017-12",
        "field_ovrigt_value": null,
        "field_presentation_value": "2017-12-18 12:15:00",
        "field_presentation4_value": null,
        "field_presentator_value": "Dana G",
        "field_presentator2_value": null,
        "field_abstract_value": null,
        "opponent_1": "Karl L",
        "opponent_2": null,
        "opponent_3": null,
        "filename": "1802_Dana_G.pdf"
    

    And the following is my code:

    
    $pod = pods( 'examensarbete' );
    $str = file_get_contents('./cleaned_data.json', true);
    $json = json_decode($str, true);
    
    foreach ($json["0"] as $index => $exjobb) {
      $filename = pathinfo($exjobb['filename'])['filename'];
      $file_POST = get_page_by_title($filename, "OBJECT", 'attachment');
    
      $data = array(
        'post_status' => 'publish',
        'pagaende_avslutat' => $exjobb['pagaende_avslutat'],
        'post_title' => $exjobb['title'],
        'rapport' => array(
          'id'    => $file_POST -> ID,
          'title' => $file_POST -> post_title),
        'forfattare_1' => $exjobb['forfattare_1'],
        'forfattare_2' => $exjobb['forfattare_2'],
        'forfattare_3' => $exjobb['forfattare_3'],
        'beraknas_fardigt' => $exjobb['field_estimated_value'],
        'handledare' => $exjobb['field_handledare_value'],
        'foretag_institution' => $exjobb['field_foretag_value'],
        'amnesgranskare' => $exjobb['field_amnesgranskare_value'],
        'ovrigt' => $exjobb['field_ovrigt_value'],
        'opponent_1' => $exjobb['opponent_1'],
        'opponent_2' => $exjobb['opponent_2'],
        'opponent_3' => $exjobb['opponent_3'],
        'presentationstid_1' => $exjobb['field_presentation_value'],
        'presentationstid_2' => $exjobb['field_presentation4_value'],
        'abstract' => $exjobb['field_abstract_value'],
      );
      $new_exjobb_id = $pod->add( $data );
    }
    

    However, running this code does not create a new post of the pods-type “examensarbete”. What am I doing wrong? I don’t get any error messages, just a blank page running this script.

Viewing 1 replies (of 1 total)
  • Thread Starter slotigork

    (@slotigork)

    I was too quick to ask this question. One of my fields was a Yes/No label with custom labels. The json file was all lower case while the pods was defined with the first letter capitalized.

Viewing 1 replies (of 1 total)
  • The topic ‘Can’t add new posts programmatically’ is closed to new replies.