Search code examples
phpdatabasewordpresscroncustom-post-type

Cron job - Moving data between databases


I have a membership website in Wordpress which is built with a custom post type called 'members'.

For many reasons, we have used a cron job to run and populate these CPT fields with data from a separate database, (it wasn't feasible for the company to use the standard WP database to store their data.

The database starts in MS Access, is exported to a separate table inside our WP database and then the cron job pulls the data from this database and adds it to the CPT database.

Inside the cron job document we have lines like this:

update_field( 'memberimage', $members->webpic, $post_id );
update_field( 'description', $members->description, $post_id );
update_field( 'phone_number', $members->telno, $post_id );
update_field( 'email_address', $members->email, $post_id );
update_field( 'website_address', $members->web, $post_id );

Which obviously take the data from the fields in the database and add it to the fields we created in CPT etc. etc.

For each member, in the backend, I have a dropdown created with advanced custom fields (field name: gold_or_silver_member with the selection options of gold or silver) which marks them as a gold of a silver member.

Silver is default and if they are gold, all sorts of conditional things happen to show more features on their directory page.

In the database we are pulling from, the column that looks after this is called webtier so at the database level, the database user adds a number 2 to the field is the member is silver and a number 3 to the field if the member is gold.

I'm trying to grab this number from the database, in the fashion above, and output it to return either gold or silver inside the ACF for each member.

We already have this line:

update_field( 'gold_or_silver_member', 'Silver', $post_id );

which, I think, makes all members silver by default, so the logic, I think, only needs to say:

if the number in the database field, webtier is 3 then change the ACF field to gold

I think anyway...

I'm a bit stuck to be honest, Im picking this up from someone else who I cant get hold of because of the current situation, so I've been tasked with figuring it out... Can anyone help and advise me?

The full cronjob.php file is:

<?php
include('wp-config.php');

global $wpdb;
$taxonomy="business_sector";
$members = $wpdb->get_results("SELECT dep.*  FROM directory_enteries_partnership as dep where status=0 ");
$i=1;
foreach($members as $members){
    //echo $members->webcat;
    //echo "<br>";
    $category = $wpdb->get_row("SELECT *  FROM directory_categories_partnership as dcp where maincategory=$members->webcat");

    //echo "$i";
    //echo "<br>";
    //$i++;
    //echo "<pre>";
    //print_r($category);
    $term = get_term_by('name',$category->description,$taxonomy);
//  echo "<pre>";
    //print_r($term);
//  print_r($members);
//  //die;
    $address =  $members->address1.' '.$members->address2.' '.$members->town.' '.$members->postcode;

$my_post = array(
     'post_title' => $members->orgname,
     'post_type'  => 'members',
     'post_status' => 'publish',
     'post_author' => 1,
     'post_category' => array( $term->term_id ),

  );

// Insert the post into the database
 $post_id = wp_insert_post( $my_post );
 wp_set_post_terms($post_id,$term->term_id,$taxonomy);
 update_field( 'logoimage', $members->weblogo, $post_id );
update_field( 'address', $address, $post_id );
update_field( 'memberimage', $members->webpic, $post_id );
update_field( 'description', $members->description, $post_id );
update_field( 'phone_number', $members->telno, $post_id );
update_field( 'email_address', $members->email, $post_id );
update_field( 'website_address', $members->web, $post_id );
update_field( 'show_as_new_member', 'No', $post_id );
update_field( 'show_as_featured_member', 'No', $post_id );
update_field( 'gold_or_silver_member', 'Silver', $post_id );
$field_key = "social_media_accounts";
$value = array(
    array(
        "social_network_name"   => "Facebook",
        "url"   => $members->facebook
    ),
    array(
        "social_network_name"   => "Twitter",
        "url"   => $members->twitter
    )
);
update_field( $field_key, $value, $post_id );
$wpdb->update( 
    'directory_enteries_partnership', 
    array( 
        'status' => 1
    ), 
    array( 'id' => $members->id )
);
// die;
}
?>

Any help or advise is massively appreciated!! Thanks for looking.


Solution

  • Replace this line update_field( 'gold_or_silver_member', 'Silver', $post_id );

    With this code:

    $webtier = $members->webtier;
    $membership = 'Silver';
    if ($webtier == 3) $membership = 'Gold';
    update_field( 'gold_or_silver_member', $membership, $post_id );