Advanced Custom Fields repeater
-
Will this plugin allow for the listing of all the posts where the image has been added to an Advanced Custom Fields repeater field? Within the repeater field, there’s a sub-field for the image, and I am trying to find a way for the media library to display which posts the image has been added to with this method.
-
Thanks for your question. I have a bit of experience with ACF but not specifically with repeater fields. There is no “where-used” feature in the current MLA version that will give you the information you’ve outlined.
It is possible that this feature could be added to the Media/Assistant submenu table as a custom view or column. I will have a look at this and post an update here with my findings.
Thanks for looking into it and thanks for a great plugin.
Thank you for your patience while I was busy with several support topics and other tasks. I did a bit of research on your question and found this in the ACF documentation:
Activating the repeater field
The repeater field is a premium add-on which can be purchased, downloaded and activated from the add-ons page.
How is the data saved?
The repeater field saves all it’s data in the wp_postmeta table. If your repeater field is called “gallery” and contains 2 sub fields called “image” and “description”, this would be the database structure of 2 rows of data:
// meta_key meta_value gallery 2 // number of rows gallery_0_image 6 // sub field value gallery_0_description "some text" // sub field value gallery_1_image 7 // sub field value gallery_1_description "some text" // sub field value
I hope you understand I cannot purchase premium themes, plugins and components just to provide MLA support. However, there may be enough information in the “How is the data saved?” entry to let me guess at a solution. Can you tell me the name of your Repeater field, the name(s) of the sub fields and give me an example of what data values are stored in each sub-field? With that information I can do more investigation.
Thanks again for your patience and understanding.
I understand about the premium plugins. Note that ACF now has an “ACF Pro” version rather than the add-ons, but from what I gather, the back-end is the same.
The field name of the repeater is “photos_alt”. The sub field for the image within the repeater is called “photo”
I used this to output the array:
$repeater = get_field('photos_alt', $thePostID); print_r($repeater);
Then here’s an example of what it spits out:
Array ( [0] => Array ( [photo] => Array ( [ID] => 1015419 [id] => 1015419 [title] => Oscars 2015 red carpet photos [filename] => x-oscars-2015-red-carpet-photos-patricia-arq-024-600.jpg [url] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-024-600.jpg [alt] => Oscars 2015 red carpet photos [author] => 4 [description] => getty [caption] => Test Caption [name] => x-oscars-2015-red-carpet-photos-patricia-arq-024-600-jpg [date] => 2015-02-23 01:45:00 [modified] => 2015-03-16 08:27:45 [mime_type] => image/jpg [type] => image [icon] => https://domain.com:8080/wordpress/wp-includes/images/media/default.png [width] => 599 [height] => 337 [sizes] => Array ( [thumbnail] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-024-600-150x150.jpg [thumbnail-width] => 150 [thumbnail-height] => 150 [medium] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-024-600-300x169.jpg [medium-width] => 300 [medium-height] => 169 [large] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-024-600.jpg [large-width] => 599 [large-height] => 337 [16x9thumb-small] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-024-600-187x105.jpg [16x9thumb-small-width] => 187 [16x9thumb-small-height] => 105 [16x9thumb] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-024-600-400x225.jpg [16x9thumb-width] => 400 [16x9thumb-height] => 225 [16x9thumb-large] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-024-600.jpg [16x9thumb-large-width] => 599 [16x9thumb-large-height] => 337 ) ) [title] => Oscar 2015 red carpet [caption] => Test Caption ) [1] => Array ( [photo] => Array ( [ID] => 1015420 [id] => 1015420 [title] => Keira Knightley [filename] => x-oscars-2015-red-carpet-photos-patricia-arq-066-600.jpg [url] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-066-600.jpg [alt] => Keira Knightley [author] => 4 [description] => getty [caption] => [name] => x-oscars-2015-red-carpet-photos-patricia-arq-066-600.jpg [date] => 2015-02-23 01:45:00 [modified] => 2015-02-23 01:45:00 [mime_type] => image/jpg [type] => image [icon] => https://domain.com:8080/wordpress/wp-includes/images/media/default.png [width] => 399 [height] => 600 [sizes] => Array ( [thumbnail] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-066-600-150x150.jpg [thumbnail-width] => 150 [thumbnail-height] => 150 [medium] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-066-600-200x300.jpg [medium-width] => 200 [medium-height] => 300 [large] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-066-600.jpg [large-width] => 399 [large-height] => 600 [16x9thumb-small] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-066-600-187x105.jpg [16x9thumb-small-width] => 187 [16x9thumb-small-height] => 105 [16x9thumb] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-066-600-399x225.jpg [16x9thumb-width] => 399 [16x9thumb-height] => 225 [16x9thumb-large] => https://domain.com:8080/wordpress/wp-content/uploads/2015/01/x-oscars-2015-red-carpet-photos-patricia-arq-066-600-399x506.jpg [16x9thumb-large-width] => 399 [16x9thumb-large-height] => 506 ) ) [title] => Kiera Nightly [caption] => Test Caption2 ) )
Thanks for understanding my position on premium plugins, and for posting the additional information on your specific application.
Based on the information you provided I have developed a small custom plugin that displays the “where-used” information for the ACF repeater field. Actually I have added this feature to an existing example plugin that also shows how to support an ACF checkbox in MLA. I won’t give the entire (532 lines of) source code here, but the interesting part required just two of the hooks provided by the Media Assistant submenu table code. The first hook adds the column to the submenu table:
public static function mla_list_table_get_columns( $columns ) { /* * Add a column of our own for the repeater "where-used" information */ $columns[ 'acf_' . self::ACF_REPEATER_FIELD ] = self::ACF_REPEATER_TITLE; return $columns; } // mla_list_table_get_columns
The
ACF_REPEATER_FIELD
andACF_REPEATER_TITLE
constants make it easy to change the field name and column title. The second hook fills the column when the table is displayed:public static function mla_list_table_column_default( $content, $item, $column_name ) { /* * Retrieve and format the repeater field "where-used" information */ if ( ( 'acf_' . self::ACF_REPEATER_FIELD ) == $column_name ) { global $wpdb; $where_clause = self::ACF_REPEATER_FIELD . '_%_' . self::ACF_SUB_FIELD; $references = $wpdb->get_results( " SELECT * FROM {$wpdb->postmeta} WHERE meta_key LIKE '{$where_clause}' AND meta_value = {$item->ID} " ); $content = ''; if ( ! empty( $references ) ) { $parents = array(); foreach ( $references as $reference ) { // key on post_id to remove duplicates $parents[ $reference->post_id ] = $reference->post_id; } $parents = implode( ',', $parents ); $references = $wpdb->get_results( " SELECT ID, post_type, post_status, post_title FROM {$wpdb->posts} WHERE ( post_type <> 'revision' ) AND ( ID IN ({$parents}) ) " ); foreach ( $references as $reference ) { $status = self::_format_post_status( $reference->post_status ); if ( $reference->ID == $item->post_parent ) { $parent = ',<br>' . __( 'PARENT', 'media-library-assistant' ); } else { $parent = ''; } $content .= sprintf( '<a href="%1$s" title="' . __( 'Edit', 'media-library-assistant' ) . ' “%2$s”">%2$s</a> (%3$s %4$s%5$s%6$s), ', /*%1$s*/ esc_url( add_query_arg( array('post' => $reference->ID, 'action' => 'edit'), 'post.php' ) ), /*%2$s*/ esc_attr( $reference->post_title ), /*%3$s*/ esc_attr( $reference->post_type ), /*%4$s*/ $reference->ID, /*%5$s*/ $status, /*%6$s*/ $parent ) . "<br>\r\n"; } // foreach $reference } // found $references } // found repeater column return $content; } // mla_list_table_column_default
The first
$wpdb->get_results
call is similar to the ACFget_field()
call in your example code. It retrieves all the rows in thepostmeta
table that reference the repeater field and sub field and have the attachment ID as themeta_value
.The second
$wpdb->get_results
call retrieves some fields in theposts
table for the posts/pages that reference the attachment. These are then formatted and returned as the column value.I will add the complete custom plugin to my next Development Version and official MLA release in the
/examples/
directory asmla-advanced-custom-fields-example.php.txt
. In the interim I can send you a copy by e-mail if you go to the Contact Us page at the FTJ web site and give me your contact information:Note that this first version of the solution computes the “where-used” references on the fly, when each item is accessed. This ensures that the information is up to date, but it involves one or two database accesses for each item in the submenu table page. It also makes sorting on the column impossible.
If the performance is an issue or if you really need to sort or filter on the column you can create a new custom field that builds the “where-used” information ahead of time and stores it in the database. If that is of interest, let me know and I can work on that alternative. This simple version should get you started in any case.
I have just uploaded a new MLA Development Version. I have added the complete custom plugin to the /examples/ directory as mla-advanced-custom-fields-example.php.txt.
You can find step-by-step instructions for using the Development Version in this earlier topic:
I will leave this topic unresolved until the new feature is released in the next MLA version. If you get a chance to try the Development Version, let me know if it meets your needs. Thanks for your interest in the plugin.
Thanks, I’ll give that a shot on my development server later this week and will get back to you, hopefully by early next week.
Initial testing on my development server has been successful. I’m hoping to install it on the live server which has a much larger media library at some point this week.
Thanks for your help on this!
I have released MLA version 2.10, which contains the complete custom plugin to the /examples/ directory as mla-advanced-custom-fields-example.php.txt.
I am marking this topic resolved, but please update it if you have any problems or further questions regarding ACF Repeater Field support in MLA.
- The topic ‘Advanced Custom Fields repeater’ is closed to new replies.