top of page

Set Featured Image from third party Programmatically

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • Jun 12, 2023
  • 1 min read

      $args = array(
          'post_type'      => 'businesses',
          'posts_per_page' => -1, 
          'post_status'    => 'publish',
          //'offset'         => 15
      );

      $query = new WP_Query($args);

      if ($query->have_posts()) {
          while ($query->have_posts()) {
              $query->the_post();

              //echo get_the_id().'<br/>';
              //$postid = 13781; 

              if( get_the_id() == $postid ){

                $imagepath = get_field('image_path');
                $photopath = get_field('photo_file');

                $imageurl  = $imagepath.$photopath; 

                echo $imageurl; 

                require_once(ABSPATH . 'wp-admin/includes/file.php');
                require_once(ABSPATH . 'wp-admin/includes/media.php');
                require_once(ABSPATH . 'wp-admin/includes/image.php');

              
                // load the image
                $result = media_sideload_image($imageurl ,$postid,'featured image', 'id' );

                update_post_meta($postid , '_thumbnail_id',$result );

                echo $result;
              }
              
          }
      } else {
          
      }


 
 
 

Recent Posts

See All
Duplicate Category in Wordpress

The following code helps to duplicate the default wordpress category by help of code. // Add Duplicate option for categories in WordPress...

 
 
 
C#.NET and Its Associated Codes

1- Creating an Desktop Application in C#.NET C# ADO.NET & MySQL MAIN C# and MySQL ADO.NET with Crystals Reports Ready Code 2- Handling...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page