wp_query for displaying attachments with a tag

So I am running the following query:

<?php
    $args = new WP_Query(array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'oderby' => 'title',
        'order' => 'ASC',
        'post_status' => 'any',
        'post_parent' => null,
        'tax_query' => array(
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => 'logo'
            )
        )
    ));

    while ( $args->have_posts() ) : $args->the_post();

?>

An issue that I need to address is a better sorting method for the attachments. Currently I can only get it by title ASC or DESC. I am aware of the other orderby attributes available with the WP_Query but not all seem to work. I was wondering if anyone knows of a way to modify the upload date of images in the media library because then I could modify my query to go based off of date and we can just edit the dates of the images to show newer ones first etc. Any ideas that could help me with getting more of a manual control of this would be great.

Thanks!

1 Answer
1

So after some more digging was able to figure it out. Using the Advanced Custom Fields plugin, I created a text field that appears on attachments with specific tag applied to them. Now you can assign a value and I updated my WP_Query to reflect:

<?php   
    $args = new WP_Query(array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'oderby' => 'meta_value_num',
        'order' => 'ASC',
        'post_status' => 'any',
        'post_parent' => null,
        'meta_query' => array(
            array(
                'key' => 'logo_sort_order'
            )
        ),      
        'tax_query' => array(
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => 'logo'
            )
        )
    ));

    while ( $args->have_posts() ) : $args->the_post();

?>

Hope this helps someone else.

Leave a Comment