TransWikia.com

Updating custom fields with external data

WordPress Development Asked by Hal Atkins on February 24, 2021

For our articles on our WordPress site, we would like to display article analytics that are collected weekly for each manuscript. We have these native custom fields for each post:

  • Article Views
  • PDF Downloads
  • PubMed Central Views
  • Article Citations

That data (which is coming from different sources) is being collected for each of our articles weekly into a CSV file. I’d like to run some sort of script each week that will take that data and update the native fields in the WordPress database (in each post) with the data coming from that CSV file.

Is there a means of writing a script that can do such a thing?

2 Answers

Please refer the tutorial explains how to read data from the CSV and create and update existing post. It also explain how to update acf fields.

http://www.pearlbells.co.uk/insert-udpate-wordpress-post-programmatically/

Paste the code for creating and updating posts.

function postExists($postCSVContent,$parentId , $mydb) {

$mydb->get_results($mydb->prepare('SELECT ID FROM `wp_posts` WHERE ID = %d',$postCSVContent[0]));

if( $mydb->num_rows >  0) {

    echo 'Post '.$postCSVContent[0].' exist'.PHP_EOL;
    $template_file = get_post_meta( $postCSVContent[0], '_wp_page_template', true );
    echo 'Template File : '.$template_file.PHP_EOL;

    $updatePost = array(   
        'ID' => $postCSVContent[0],
        'post_title'    => $postCSVContent['1'],
        'post_content'  => $postCSVContent['2'],
        'post_type' => 'doors',
        'post_status'   => 'publish',
        'post_author'   => 1
    );

    wp_update_post( $updatePost );

    update_post_meta( $postCSVContent[0], '_wp_page_template', $template_file );
    updateAcf( $postCSVContent , $postCSVContent[0] );

}
else
{
    echo 'Post '.$postCSVContent[0].' is not existing'.PHP_EOL;        
    echo 'Post parent '.$parentId.PHP_EOL;

    $newIds = wp_insert_post( array(
            'post_title' => $postCSVContent['1'],
            'post_content' => $postCSVContent['2'],
            'post_type' => 'doors',
            'post_status' => 'publish',        
            'post_author'   => 1,
            'post_parent' => $parentId
    ));

    updateAcf( $postCSVContent , $newIds );
    return $newIds;

}

Answered by Liz Eipe C on February 24, 2021

You can use PHP's fgetcsv() or str_getcsv() function to parse the uploaded CSV file. Your CSV will need to include a column with the Post's ID as well as the columns matching up to your custom field names. Loop through the CSV and for each line update the custom fields for that post using update_post_meta()

http://php.net/manual/en/function.fgetcsv.php

http://php.net/manual/en/function.str-getcsv.php

http://codex.wordpress.org/Function_Reference/update_post_meta

Answered by karpstrucking on February 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP