Tuesday 3 April 2018

Set Featured Images Programatically in WordPress – Robert DeVore


Recently, I found myself needing to auto-set 300+ featured images for a customers WooCommerce store. After the initial panic phased itself out, I took a couple of deep breathes and realized I’ve done a few kinda-sorta similar things with custom functions in the past.
After a few hours of hacking the code up and reformatting it to fit my specific needs, I ended up with the plugin that automatically cycles through each WooCommerce product and does it’s magic on plugin activation.
Why set featured images programatically?
If you’re working on a client website and there’s only a couple of featured images you need to set, then it may be best to just manually set each featured image.
But let’s say your client has over 200 WooCommerce products that are all set up, except for the featured images.
Let’s say the client added images to each product, but placed them inside the content instead of setting a featured image.
What direction would you take to handle this situation?
The first option would be to add a featured imaged to each product manually.
Instead, the code below will cycle through all of your published WooCommerce products and get the first image
Source: https://managewp.org/articles/17301/set-featured-images-programatically-in-wordpress-robert-devore



source https://williechiu40.wordpress.com/2018/04/03/set-featured-images-programatically-in-wordpress-robert-devore/

No comments:

Post a Comment