I thought about it again and there is an even easier way without a completely custom query. Just use the bricks/posts/query_vars hook.
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
if ( $element_id === 'cghjvv' && function_exists( 'get_field' ) ) {
$gallery = get_field( 'gallery' );
if ( empty( $gallery ) ) {
/* Make sure that no posts are returned when
* there are no images in the gallery
*/
$query_vars['post__in'] = [0];
return $query_vars;
}
/* Get the first gallery item to automatically determine
* the ACF gallery field's return format
*/
$test_item = current( $gallery );
if ( is_array( $test_item ) ) {
// Get image IDs from image arrays
$gallery = array_map( function( $image_array ) {
return $image_array['ID'];
}, $gallery );
} else if ( is_string( $test_item ) ) {
// Try to get image IDs from image urls
$gallery = array_map( function( $image_url ) {
return attachment_url_to_postid( $image_url );
}, $gallery );
}
$query_vars['post__in'] = $gallery;
}
return $query_vars;
}, 10, 3 );
Just change the element ID in my snippet (cghjvv) to the ID of your query loop elementās ID. And change gallery to the name of your custom gallery field.
Hey Mehdi,
This is not a bug report, but a feature request I will move the thread.
Official feature requests are still to be submitted via the Idea Board: https://bricksbuilder.io/ideas/
This way we can see how many users have a need for this feature. Until then, I think Andreās code is very easy to use.
You donāt have to, thatās what you have @aslotta for
Thank you for your reply, and also thanks to @aslotta
I thought its a quite useful feature and its easy for you guys to implement
Also almost in every website with a cpt (real estate, services, portfolio and etc) we need a gallery
And Nestable slider would be a great choice to render the results
Awesome help thanks aslotta. If we wanted to use this query across a few different elements, is there a better/cleaner way than targeting the element ID(s)?
Also, are you choosing the post type that contains the ACF when you pick āMedienā please? When I do that, it just loops through the posts in that post type instead
Iām using JetEngine but i canāt figure it out how I can make this work. In the database I have the meta field ābildergalerieā with the image IDs as the meta_value (4601,4600,4599). I tried it with this function but it didnāt work:
Would it be better to change the meta_value from āMedia IDā to āArray with media ID and URLā? And do I habe to use the ID with the prefix ābrxeā or without?
I have tweaked @aslotta code a bit. It was try an error but this is working.
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
/* Place your loop element id */
if ( $element_id === 'cewxop' ) {
global $post;
$gallery = get_post_meta( $post->ID, 'gallery', true ); // JetEngine media meta field (array with ID)
if ( empty( $gallery ) ) {
/* Make sure that no posts are returned when
* there are no images in the gallery
*/
$query_vars['post__in'] = [0];
return $query_vars;
}
// Convert the gallery IDs string into an array
$gallery_ids = array_map( 'intval', explode( ',', $gallery ) );
$query_vars['post__in'] = $gallery_ids;
}
return $query_vars;
}, 10, 3 );
Hi,
This code works , it is modified for jetengine.
In field settings, select value format as Array with media ID and URL
$jetengine_images = get_post_meta( get_the_ID(), 'my-galerie', true );
// Check if the array is not empty
if (!empty($jetengine_images)) {
// Extract only the IDs of the images
$image_ids = array_map(function($item) {
return $item['id']; // Ensure that the key for IDs is correct
}, $jetengine_images);
// Use these IDs in the WP_Query
return [
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'orderby' => 'post__in',
'post__in' => $image_ids,
'posts_per_page' => -1,
];
} else {
return [];
}