• Resolved jtm12

    (@jtm12)


    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))); 
    ?>
    • This topic was modified 2 years, 8 months ago by bcworkz. Reason: code format fixed
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The “meta_key” arg should be assigned the actual key name, not the returned value from get_post_meta(). Hence:
    'meta_key' => '_wp_attachment_image_alt',

    You don’t need a “meta_value” arg unless you wish to restrict results to a specific meta value.

    Thread Starter jtm12

    (@jtm12)

    I made the changes, including deleting the meta_key line completely, and the code below worked perfectly! Thank you.

    
    <?php $query_images_args = array(
    'post_type'      => 'attachment',
    'category_name'  => 'colors',
    'post_mime_type' => 'image',
    'post_status'    => 'inherit',
    'posts_per_page' =>  30,
    'orderby'        => '_wp_attachment_image_alt',
    '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)));
    ?>
    `

    Thread Starter jtm12

    (@jtm12)

    This was resolved.

    Thread Starter jtm12

    (@jtm12)

    I need to correct the code above.

    Here is the code that works for me to order images from the media library by the alt tag:

    
    <?php $query_images_args = array
    'post_type'      => 'attachment'
    'category_name'  => 'colors',
    'post_mime_type' => 'image',
    'post_status'    => 'inherit',
    'posts_per_page' =>  30,
    'meta_key' 	 => '_wp_attachment_image_alt',
    'orderby'        => '_wp_attachment_image_alt',
    '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))); 
    ?>
    <?php endwhile; ?>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>

    `

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.