Ahmad Naser Turnkey Ecosystem Ahmad Naser
November 19, 2019

ACF Import WordPress Custom Taxonomy images

Published in the Ahmad Naser ecosystem. Estimated reading time: 2 minutes.

      function import_terms_icons() {
    // do stuff here
	
	
$url = "https://example.com/wp-content/functions/arabic-cats.json";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
//print_r($json_data);


	
	
            $args = array(
                'taxonomy'     => 'job_listing_category',
                'orderby'      => 'name',
                'show_count'   => 1,
                'pad_counts'   => false,
                'hide_empty'               => 0,
                'hierarchical'             => 1,
                'exclude'                  => '',
                'include'                  => '',
                'number'                   => '',
                'hierarchical' => 1,
                'echo'         => true
            );

			
// Getting Categories			
$_categories = get_categories($args);

//sample of single term insert
/*		
$cc = $_categories[0];
$file_url = 'https://example.com/wp-content/uploads/2019/11/asian-cars.png';
$wp_upload_dir = wp_upload_dir();
$filetype = wp_check_filetype( basename( $file_url ), null );
		$attachment = array(
			'guid'           => $wp_upload_dir['url'] . '/' . basename( $file_url ), 
			
			'post_parent' 	 => $cc->term_taxonomy_id,
			'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ),
			'post_type' 	 => 'attachment',
			'post_content'   => '',
			'post_status'    => 'inherit',
			'post_mime_type' => $filetype['type'],
		);
$attach_id = wp_insert_attachment( $attachment, $file_url );
update_field("field_5bcaebaa7db8b", $attach_id, 'job_listing_category_'.$cc->term_taxonomy_id );
*/

$i = 0;
$wp_upload_dir = wp_upload_dir();
foreach( $_categories as $category ) {
foreach( $json_data as $categoryx ) { 
if($categoryx["Term Name"] == $category->name ){
if($categoryx["Category Icon"]){
//echo  $categoryx["Category Icon"];//Term Name



$file_url = $categoryx["Category Icon"];
$filetype = wp_check_filetype( basename( $file_url ), null );
		$attachment = array(
			'guid'           => $wp_upload_dir['url'] . '/' . basename( $file_url ), 
			
			'post_parent' 	 => $category->term_taxonomy_id,
			'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ),
			'post_type' 	 => 'attachment',
			'post_content'   => '',
			'post_status'    => 'inherit',
			'post_mime_type' => $filetype['type'],
		);
$attach_id = wp_insert_attachment( $attachment, $file_url );
$field_acf_static_name = 'field_5bcaebaa7db8b';
update_field($field_acf_static_name, $attach_id, 'job_listing_category_'.$category->term_taxonomy_id );






$i++;
echo "</br>".$categoryx["Term Name"]." - ".$i."</br>";
}
}
}
			
//code for printing out acf value			
/*
 $image_id = get_field( 'category_icon', 'job_listing_category_'.$category->term_taxonomy_id, false);
 $image = wp_get_attachment_image_src($image_id, $size);
     echo '<img src="'.$image[0].'" />';
*/
			
			}
			exit();
}
//add_action('wp_head', 'import_terms_icons');

Leave a Reply

Your email address will not be published. Required fields are marked *