• dpope0824

    (@dpope0824)


    I am looking to query the media files and set a loop to display them. Is there any way to do this? I had to do a batch rename of files that now all have the suffix *_post-1.jpg. I dont think there is but being able to set a for each loop would make adding the total of over 500 images to a grid view so much easier than manually adding every image.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Luis Sacristán

    (@displaynone)

    Hi, you should use $wpdb->get_results, I’m telling you by heart:

    $results = $wpdb->get_results("select * from {$wpdb->posts} where post_type = 'attachment' and guidance like '%_post-1.jpg'");

    Hope it helps

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I think you got autocorrected, Luis. “guidance” should be “guid“.

    Luis Sacristán

    (@displaynone)

    Thanks Steve, you’re right, it’s guid

    Thread Starter dpope0824

    (@dpope0824)

    Thanks a lot, that put me on a really good path. Always good to see a thriving community.

    Thread Starter dpope0824

    (@dpope0824)

    This ended up being my little snippet of code and using a custom field from ACF to add whichever suffix I needed.

    <?php
    global $wpdb;
    $file_suffix = get_field( "file_suffix" );
    
    $result = $wpdb->get_results ( "
        SELECT * 
        FROM  $wpdb->posts
            WHERE post_type = 'attachment'
    		AND guid LIKE '%$file_suffix'
    " );
    
    foreach ( $result as $page )
    
    {
    				$url = get_the_guid($page);
    
      echo '<a href="'.$url.'"><img src="'.$url.'"></img></a><br/>';
    
    }
    ?>
    Thread Starter dpope0824

    (@dpope0824)

    Now I get to figure out a way to paginate the results. So it will turn them into a slideshow of sorts.

    Luis Sacristán

    (@displaynone)

    Use wp_rewrite and the query var paged

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Query Media files by item name’ is closed to new replies.