functions – Featured Image and Tags problem


I am very new to PHP and am having an issue with an amazing advanced WordPress tutorial which showed how to use ACF and functions.php to scrape and import the data directly into my Animal Shelter theme. The api is scraped and a majority of the data is imported however the featured image is causing issues, as are the tags.
The way the animals images are retrieved is with a set url with the animal ID at the end and I wish to simply place the relevant image for the relevant animal in each record.

Here is my code:

$animals[] = $results;

foreach( $animals[0] as $animal ){

    $animal_slug = sanitize_title($animal->ANIMALNAME . '-'. $animal->ID);

    **$animalID_REF = get_field('ID');
    function filter_post_thumbnail( $html ){
        if ('' == $html){
            return'<img src="****&method=animal_image&animalid=<?=$animalID_REF;?>" />';
        //Else, return the post thumbnail
        return $html;
    add_filter( 'post_thumbnail_html','filter_post_thumbnail' );**

    $animal_title = sanitize_title($animal->ANIMALNAME);

    **$animal_tag = sanitize_title($animal->PETFINDERSPECIES);**

    $existing_animal = get_page_by_path($animal_slug, 'OBJECT', 'project');

    if( $existing_animal === null){

    $inserted_animal = wp_insert_post([
        'post_name' => $animal_slug,
        'post_title' => $animal_title,
        **'pj-tags' => $animal_tag,**
        'post_type' => 'project',
        'post_status' => 'publish'

    if( is_wp_error( $inserted_animal) ){
        echo $inserted_animal->get_error_message();
    $fillable = [

        'field_62308905b1063' =>'ANIMALNAME',
        'field_6230967891d2d' =>'PETFINDERSPECIES',            
        'field_62308905b4b0d' =>'ID',
        'field_62308905b857f' =>'BREEDNAME',
        'field_623096f191d2e' =>'SEXNAME',
        'field_6230970a91d2f' =>'AGEGROUP',
        'field_6230972591d30' =>'ISGOODWITHCHILDRENNAME',
        'field_6230973191d31' =>'ISGOODWITHCATSNAME',
        'field_6230973e91d32' =>'ISGOODWITHDOGSNAME',
        'field_6230974b91d33' =>'ISHOUSETRAINEDNAME',
        'field_6230975a91d34' =>'ISNOTAVAILABLEFORADOPTIONNAME',         
        'field_62309281825e3' =>'LASTCHANGEDDATE',

    foreach( $fillable as $key => $name){
        update_field( $key, $animal->$name, $inserted_animal );
}else {

   $existing_animal_id = $existing_animal->ID;
   $existing_animal_timestamp = get_field('LASTCHANGEDDATE', $existing_animal_id);

   if( $animal->LASTCHANGEDDATE >= $existing_animal_timestamp){
        //update our post meta


As you can see I tried using get_FIELD ID from ACF to reference the animal and add its ID to the end of the URL but as you may be able to tell I was way off from ever managing to display the animals from their external URL in WP. Incidentally I do have a plugin called FIFU to reference external urls but again unsure how I would incorporate this in this instance.

My second failure comes in the form of tags and my theme uses pj-tags for it´s tags. Again I tried populating these from the ACF fields, in this case PETFINDERSPECIES.

Can anyone steer me in the right direction please?

user3069831 4 months 2022-04-07T11:06:45-05:00 0 Answers 0 views 0

Leave an answer