Filter the results of Image Attributes Pro’s image search.
Image Attributes Pro attempts to a comprehensive search to find out where an image is used. As of version 4.8, Image Attributes Pro looks for an image in the following locations.
- Featured image of a post, page or product (or any registered custom post type).
- HTML content of posts, pages or products.
- Checks if the image is uploaded to a post (denoted as “Uploaded to” in the Media Library).
On top of this, if the website uses WooCommerce, the search is extended to the following locations as well. Internally this filter is used to search WooCommerce specific locations.
- WooCommerce product variation image.
- WooCommerce product gallery.
/**
* Filter $posts array.
* Used in /3rd-party/woocommerce.php to add search in WooCommerce products.
*
* @since 4.8
*
* @param $posts (array) An associative array of posts where the image is used.
* @param $attachment_id (int) ID of the image.
*/
$posts = apply_filters( 'iaffpro_get_posts_by_attachment_id', $posts, $attachment_id );
The $posts is an associative array with it’s keys starting with a number.
10_thumbnail: If the image is used as a featured image, then that has the highest preference.20_wc_product_variation_image: WooCommerce product variation image.30_wc_product_image_gallery: WooCommerce product where the image is part of the product gallery.40_content: Posts where image is used within the content.50_uploaded_to: The post ID to with the image is uploaded to.
When an image is used in multiple locations, the post / product associated with the lowest number will be considered to generate image attributes.
Also refer related filter, iaffpro_image_used_in_text.
Example Usage
Let’s say you have a listing website that uses a non WordPress system to create a listing. One image is set as the main image for the listing. Let’s also assume that this image is possibly used in other parts of your website. Maybe in a WordPress post.
Out of the box, Image Attributes Pro will find the post on which the image is used and will use it to generate image attributes. For instance, %posttitle% custom image attribute tag will be the title of the post and not that of the listing as it should be.
To find listings where the image is used and to add it to Image Attributes Pro’s search results, your code could look like so.
/**
* Find Listings where an image is used.
*
* @link https://imageattributespro.com/codex/iaffpro_get_posts_by_attachment_id/
*
* @param $posts (array) An associative array of posts where the image is used.
* @param $attachment_id (int) ID of the image.
*
* @return $posts (array) Array with Listings added to the original $posts array.
*/
function prefix_iap_get_my_listings_by_attachment_id( $posts, $attachment_id ) {
$listings = array();
// Your database query to find listings goes here.
$listings = array_unique( $listings );
$posts['05_my_listings'] = $listings;
return $posts;
}
add_filter( 'iaffpro_get_posts_by_attachment_id', 'prefix_iap_get_my_listings_by_attachment_id', 10, 2 );
Note that the array key (05_my_listings) starts with a number (05 in this example) and it’s lower than the first key of the default array, which is 10_thumbnail. This will tell Image Attributes Pro to give listings as the highest priority while generating image attributes for that specific image.