WP Grid Builder populate grid from custom field (ACF gallery)

Viewed 632

I would like to populate WPGRIDBUILDER grid from ACF Gallery field.

Support told me:

Grids only work with WOrdPress content type: (custom) post types, (custom) taxonomy terms and WordPress users. So, what you are looking for requires to use custom PHP code in order to populate a grid from ACF field. And the field must hold image IDs uploaded in WordPress media library.

Here are two snippets which I assume can be the answer. I'm totally newbie in PHP.

https://docs.wpgridbuilder.com/resources/filter-grid-query-args/

function prefix_query_args( $query_args, $grid_id ) {

    if ( 1 === $grid_id ) {
        $query_args['post__not_in'] = [ 1, 2, 3, 4 ];
    }

    return $query_args;
}

add_filter( 'wp_grid_builder/grid/query_args', 'prefix_query_args', 10, 2 );

https://support.advancedcustomfields.com/forums/topic/gallery-field-return-images-ids/

$images = get_field('gallery_field'); //gallery field
   foreach( $images as $img ):
   $img_srcset = wp_get_attachment_image_srcset($img['ID']); // return Image ID for each in gallery

Here is my take on it:

function prefix_query_args( $query_args, $grid_id ) {

    if ( 2 === $grid_id ) { //mygrid id
        $query_args['post_type'] = [ 'realizacje-cpt' ];//my cpt
        $images = get_field('gallery-cf'); //gallery field
        foreach( $images as $img )
          $img_srcset = wp_get_attachment_image_srcset($img['ID']);
    }

    return $query_args,$images;

}

add_filter( 'wp_grid_builder/grid/query_args', 'prefix_query_args', 10, 2 );

Em I even going in right direction?

0 Answers
Related