How to: images from media library, wp_get_attachment_image_src, custom PHP show them in a loop in Pinegrow

Hi, (read further in my answer on this item)
With Pinegrow we can loop through posts and pages…
But I see the images in the media library can be assigned to a media category.
Can I loop these images in Pinegrow?

image

I will make another Forum post since I need to re-write my question I think in a better way.
This item matches with this question:
How can I use custom PHP to show posts with images from the media library.
Something like this works, but I need to integrate it in PG:
it has to do with attachments in WordPress:

Situation:
I have images in the Media Library.
In the media library I also choose a category to where the media belongs to.
When i hover over category I see the ID from the category, for example: 15.

Then this code will fetch all my images with category number 15:
I use “echo” for testing purposes of course not to have an output in PineGrow…
so this is the pure PHP that works.

Now: why can’t I get it working with a POST loop on a page… like a category page, a page, a post without being dependent of the current main loop like you normally would have for a page, post or else…

// get the posts (i.e. your images)
$images = get_posts(array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',    
    'posts_per_page' => -1,
    'category'=>15,
));

// Loop through images
foreach ($images as  $image) { ++$i;
    $img_data = wp_get_attachment_image_src($image->ID,'full');

    $src = $img_data[0];
    $description = $image->post_content;
echo $i." - ".'<a href="'.$src.'">' . "$description</><br>";}?>
<?php get_footer(); ?>

Step further… how to write my own arguments for WP_QUERY in PG?
Tested this PHP that works, now have to see how to get this into my Pinegrow code:


$args = array(    'post_type' => 'attachment', // Only bring back attachments
    'category_name' =>'3d',
    'post_mime_type' => 'image', // Only bring back attachments that are images
    'posts_per_page' => '-1',
    'post_status' => 'inherit', //
    'orderby' => 'rand', // Order the attachments randomly  
    );
 
$my_query = new WP_Query( $args );


if ( $my_query->have_posts() ) :   // pinegrow created code does not have my_query befroe have_posts ?>

@matjaz Args in own PHP block
I saw this article… I thought that is something I need in my case, but …
I dont see the options in THE LOOP you showed here compared with what I see


(sorry for disturbing in the summer :slight_smile: )

Found my solution in Pinegrow, Thanks to this article: Args in own PHP block

My problem was:
I didnt see the ARGS IN BLOCK option like it was shown in the forum post. I saw this:

Fix is to type a QUERY NAME, then it will appear:

I needed these args:
‘post_type’ => ‘attachment’,
‘category_name’ => ‘3d’,
‘post_mime_type’ => ‘image’,
‘posts_per_page’ => ‘5’,
‘post_status’ => ‘inherit’,
‘orderby’ => ‘rand’

In the Pinegrow created PHP block I did not need to enter it manually. This is an advantage since every time you change something in this WordPress Action The Loop, then your PHP block gets resetted. My parameters went in like this:

Now my next challenge is: the Pagination Action doesn’t work, maybe another topic :slight_smile: