TransWikia.com

Wordpress retrieve media except cropped

WordPress Development Asked by adviner on January 12, 2021

I’m retrieving all the images in the Media using the following code:

$query_images_args = array(
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'post_status'    => 'inherit',
    'posts_per_page' => - 1,
    'order'         => 'ASC'
);

$query_images = new WP_Query( $query_images_args );

// Get all the available thumbnail sizes
$sizes = get_intermediate_image_sizes();

$images = array();
$thumbnails = array();
foreach ( $query_images->posts as $image ) {

    $image_meta = wp_get_attachment_metadata($image->ID);
    $image_size_url = wp_get_attachment_image_src($image->ID, 'thumbnail');
    var_dump($image_size_url);
    $images[] = $image_size_url;
}

It retrieves the images but it’s also retrieving the cropped files which WordPress creates when I DND the original images into the Media section. Is there a way to prevent querying these images aside from checking for the word "cropped-" at the beginning of the filename? Or would I use apply_filters() to filter those images out?

Update 1

Looking at the query I modified it so it works as follows:

SELECT wp_posts.* FROM wp_posts WHERE
1=1 AND (wp_posts.post_mime_type LIKE ‘image/%’)
AND wp_posts.post_type = ‘attachment’
AND ((wp_posts.post_status = ‘inherit’))
AND (guid not like ‘%cropped-%’)
ORDER BY wp_posts.post_date ASC

so it works as expected. But when I modify my code to use it:

$query_cropped = array (
‘key’ => ‘guid’,
‘value’ => ‘%cropped-%’,
‘compare’ => ‘NOT LIKE’
);

$query_images_args = array(
'post_type'      => 'attachment',
'post_mime_type' => 'image',
'post_status'    => 'inherit',
'posts_per_page' => - 1,
'meta_query'    => array(
    'relation'  => 'AND',
    array ( $query_cropped )
),
'order'         => 'ASC'
);

I now return nothing. Below is the output SQL:

SELECT   wp_posts.*
FROM wp_posts  
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1  AND (wp_posts.post_mime_type LIKE 'image/%')  AND ( 
  ( 
    ( wp_postmeta.meta_key = 'guid'
AND wp_postmeta.meta_value NOT LIKE '{27a12ba6b79a4fb4cafbbe0325faa192472b0064b3a417b89150e87e1ce35cec}\{27a12ba6b79a4fb4cafbbe0325faa192472b0064b3a417b89150e87e1ce35cec}cropped-\{27a12ba6b79a4fb4cafbbe0325faa192472b0064b3a417b89150e87e1ce35cec}{27a12ba6b79a4fb4cafbbe0325faa192472b0064b3a417b89150e87e1ce35cec}' )
  )
) AND wp_posts.post_type = 'attachment' AND ((wp_posts.post_status = 'inherit')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC 

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP