Get Images from Media Library and Sort by Alt Tag
-
I’m trying to get images from the media library with a specific category and then sort them by the alt tag (because some titles have an asterisk at the start of the title, which is ruining sorting by title). There are workarounds as far as removing the asterisk in the title, but I’m obsessed with getting this now.
I can show the alt text in the image itself using
get_post_meta(get_the_ID(), '_wp_attachment_image_alt', true)
, but I’m not successful in using the same code in the this query:<?php $query_images_args = array( 'post_type' => 'attachment', 'category_name' => 'colors', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => 30, 'meta_key' => get_post_meta(get_the_ID(), '_wp_attachment_image_alt', true), 'orderby' => 'meta_key', 'order' => 'ASC' ); $query_images = new WP_Query( $query_images_args ); if($query_images->have_posts()) : while($query_images->have_posts()) : $query_images->the_post(); ?> <h4><?php the_title(); ?></h4> <?php echo $images = wp_get_attachment_image( $query_images->posts->ID, 'large', '', array( "class" => "img-fluid mx-auto d-block", "alt" => get_post_meta(get_the_ID() , '_wp_attachment_image_alt', true))); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Get Images from Media Library and Sort by Alt Tag’ is closed to new replies.