Home › Forums › Add-ons › Repeater Field › Repeater & order by image id › Reply To: Repeater & order by image id
ok: i’ve come this far.
I used the script for Querying the database for repeater sub field values. I deleted the meta_value part because i dont have a meta value, i only have the thumbnails (the value is the path to the thumbnail). So it should view all images used in the sub field thumbnail in the repeater gallery_repeat.
It shows all images, but in multiple numbers.. (it shows the one repeater 3 times, the other one 5 times, …)
this is my code: (source: http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/ )
<?php
// get all rows from the postmeta table where the sub_field (type) equals 'type_3'
// - http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE meta_key LIKE %s
",
'gallery_repeat_%_thumbnail' // meta_name: $ParentName_$RowNumber_$ChildName
));
// loop through the results
if( $rows )
{
foreach( $rows as $row )
{
// for each result, find the 'repeater row number' and use it to load the image sub field!
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'gallery_repeat_' . $matches[0] . '_thumbnail'; // $matches[0] contains the row number!
// use get_post_meta to load the image sub field
// - http://codex.wordpress.org/Function_Reference/get_post_meta
$image_id = get_post_meta( $row->post_id, $meta_key, true );
// load image src
// - http://www.advancedcustomfields.com/resources/field-types/image/
$src = wp_get_attachment_image_src( $image_id, 'full' );
?>
<div class="item">
<img src="<?php echo $src[0]; ?>" />
<p class="wp-caption-text">From
<a href="<?php get_permalink( $row->post_id ); ?>">
<?php echo get_the_title( $row->post_id ); ?>
</a>
</p>
</div>
<?php
}
}
?>
and a screenshot to show how it looks now:
As you can see, it shows multiple identical images. in my thumbnails theres only one per repeater (all unique).
It looks like it should be an easy fix, but my knowledge does not reach this far. Would help me out greatly if this was resolved!
edit: ive noticed that everytime i edit the post, a dublicate of the image is made for some reason…
regards,
Mathieu
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.