0

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="https://eur02b.sheltermanager.com/service?account=bp****&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_624956d21e12a'=>'ANIMALAGE',
        'field_6230970a91d2f' =>'AGEGROUP',
        'field_6230972591d30' =>'ISGOODWITHCHILDRENNAME',
        'field_6230973191d31' =>'ISGOODWITHCATSNAME',
        'field_6230973e91d32' =>'ISGOODWITHDOGSNAME',
        'field_6230974b91d33' =>'ISHOUSETRAINEDNAME',
        'field_6230975a91d34' =>'ISNOTAVAILABLEFORADOPTIONNAME',         
        'field_62309281825e3' =>'LASTCHANGEDDATE',
        'field_6239ff0adeea3'=>'ANIMALCOMMENTS',
        'field_6246dbde5e610'=>'WEBSITEIMAGECOUNT',
        'field_62496a5963458'=>'SPECIESNAME',
        'field_6249c945648fc'=>'HASACTIVERESERVENAME',
        'field_6249c964648fd'=>'HASTRIALADOPTIONNAME'
    ];

    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?

1 Answer 1

0

For adding the ID at the end of the URL being generated in yout filter_post_thumbnail function you would use concatenation to append the variable to the end of the URL in the return statement.

return'<img src="https://eur02b.sheltermanager.com/service?account=bp****&method=animal_image&animalid="' . $animalID_REF . '"/>';

Also you'll need to upload the image to the media library and then set it as a featured image or create a ACF field and add it to that field somehow. The filter post_thumbnail_html isn't going to set the posts featured image for you. You can read more about the post_thumbnail_html in the docs here: https://developer.wordpress.org/reference/hooks/post_thumbnail_html/

There is a answer/question thread that touches on that topic here: How do I set a featured image (thumbnail) by image URL when using wp_insert_post()?

Regarding the tags, pj_tags is not a parameter of wp_insert_post the parameter for inserting tags through wp_insert_post would be tags_input You can read more about that function on the WP docs: https://developer.wordpress.org/reference/functions/wp_insert_post/

Check out the docs to better understand what the filters and functions you're using are doing and what is available to you within them.

1
  • Thank you for the assist. I will look into the links you posted. Apr 8 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.