Support

Account

Home Forums Add-ons Repeater Field Repeater & order by image id

Solving

Repeater & order by image id

  • Hi,

    I have a repeater that gives a thumbnail, some info, and a sub repeater adding images.

    on one page i only show the thumbnails. Right now when i add one in a previous post, it does not show up as ‘new’ on the top of the page, but with the rest of the thumbnails added in this specific post.

    my code:

    <?php while ( have_posts() ) : the_post() ?>
    
    	
    <?php if( have_rows('gallery_repeat') ): ?>
    	<?php while( have_rows('gallery_repeat') ): the_row(); 
    		$image = get_sub_field('thumbnail');
    		?>
    
    <div class="item"><a href="<?php echo the_permalink(); ?>"><img src="<?php echo $image['url']; ?>" /></a></div>
    	<?php endwhile; ?>
    
    <?php endif; ?>
    
    <?php endwhile; ?>

    an explaining screenshot:
    screenshot
    thanks,
    Mathieu

  • 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:
    screenshot

    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

  • I was able to fix this issue using this source:
    http://support.advancedcustomfields.com/forums/topic/querying-database-for-repeat-sub-fields-displaying-revisions/

    on another note: would it be possible to sort it now by meta_id? so that every added image shows up first in stead of last?

    regards!
    M

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Repeater & order by image id’ is closed to new replies.