finding “where used” in custom field
-
I am trying to see which files are referenced not as attachments in a post, but as a definition in a custom field of a post. I have tried reading docs and the forums here, but I am unable to figure it out. How would I add a custom field for the plugin to look into to determine if the image was referenced in that or not?
-
Update here…I followed the directions here to try it. I am able to see the field, but nothing is populated. That said, I am using ACF, and those fields don’t show up as options in MLA, so I had to map to the custom field the ACF field was mapped to. You can see my screenshot here. The field shows up now in the MLA columns, but nothing is populated. All I am trying to do is ensure I don’t can delete images that seem duplicated, and ensure I use proper sized ones, by finding out which posts an image is used in via custom field assignment.
Hope that makes sense… any help would be greatly appreciated!
Thanks for your question, and for the update with the screenshot; very helpful. Thanks as well for taking the time to find and follow the earlier support topic (Show Image Size in Directory?).
Regarding your custom field mapping rule, I noticed that you picked “post_id” in the “Data Source” dropdown list. You should change that back to the default “- None (select a value) -” setting so you don’t overwrite the value ACF puts in this field with the ID of the Media Library item. All you want the mapping rule to do is add the ACF field to your Media/Assistant submenu table.
Regarding your ACF field definitions, I noticed that both of the fields you show are Type “image” fields. For this field type ACF populates the custom field value with the Media Library ID value for the image you’ve selected using the ACF user interface. ACF uses the ID value to retrieve and display the actual image, e.g., the image thumbnail on the Media/Edit Media screen. You can find more information in this earlier topic and the topics it links to:
displaying ACF values in front end
You wrote “The field shows up now in the MLA columns, but nothing is populated.” On my test system I defined an ACF image field and an MLA custom field mapping rule to display it as a table column. When I edit a Media Library item and add an image to the ACF field, the ID value shows up in the table column. That’s because the custom field is associated with a Media Library item, not a page or post.
My guess is that you are adding the “Header Image” and “Header Image Alt” fields to non-Media Library items, e.g. to pages and posts; is that right? In that case, nothing will show up in the Media/Assistant submenu table because the ACF custom field is associated with the page or post, not the Media Library item. To get “where used” information for these cases you will have to write some custom code to map the ACF values back to the Media Library items they use. The “MLA Advanced Custom Fields Example” plugin shows how to get “where used” information from an ACF “repeater” field. This approach could be adapted to the “image” field.
Let me know if I understand your application/question correctly and if you need more specific help with the code in the example plugin. Thanks for your interest in the plugin.
Thanks David – very thorough response and it’s much appreciated. You are correct in your guess – that’s exactly what we are doing – assigning the media to a custom field within a post. So, we just need to get a where’s used mapping for those.
I took a look at the custom plugin. I’m not very familiar with coding within wordpress.
I got as far as changing the repeater field definition – I just kept it as repeater and changed the field definitions to:
/** * Field name of the "parent" repeater field * * @since 1.02 * * @var string */ const ACF_REPEATER_FIELD = 'search_bar_image'; /** * Field Label of the "parent" repeater field * * @since 1.02 * * @var string */ const ACF_REPEATER_TITLE = 'Header Image';But the subfield and the checkbox don’t apply. I imagine the checkbox is fine, but because this isn’t a repeater, the sub-field seems to break the plugin for my intended use. I followed the rest and these are columns now but nothing shows up like they do for “inserted in”, which is effectively what I am trying to achieve – to show all the posts these are “inserted in” to the ACF field definition for Header Image (title) and search_bar_image (field name).
Thanks for any further guidance you could provide. Much appreciated!
Thanks for your update, and thanks for taking the time to dig into the example plugin.
It’s been a while since I looked at that example plugin. Let me get back into it and see what I can do. I will post an update here when I have progress to report.
Thanks for your patience. I have made some progress on this and updated the example plugin to handle “where-used” analysis for the ACF “image” field type.
I have uploaded a new MLA Development Version dated 20200420 that includes an updated “MLA Advanced Custom Fields Example” plugin. You can find step-by-step instructions for using the Development Version in this earlier topic:
PHP Warning on media upload with Polylang
Once the Development Version is installed you must update and verify the example plugin. Navigate to the Settings/Media library Assistant Documentation tab and click the “Example Plugins” button. Type “ACF” in the text box and click “Search Plugins” to filter the table.
You are looking for the “MLA Advanced Custom Fields Example” plugin. Find that plugin and hover over the title in the left-most column. Click the “Update” rollover action, then go to the WordPress Plugins/Installed Plugins submenu and verify the example plugin as you would any other plugin. Make sure you have the latest version, 1.04, installed.
Once the Development Version and updated example plugin are installed and active you can find “Header Image” and “Header Image Alt” columns added to the Media/Assistant submenu table. For those Media Library items used in these ACF image fields you will see the name and type of each post/page where the item is used. You can click on the name to edit the post/page.
You will also see “ACF Checkbox” and “Photos Alt” columns. These can be disabled by editing the
ACF_CHECKBOX_ENABLEDandACF_REPEATER_ENABLEDconstants in the source code. You can also change the list of “image” field type columns by editing theACF_IMAGE_FIELDSandACF_IMAGE_TITLESconstants in the source code.It would be great if you can install the Development Version and updated example plugin and let me know how they work for you. Thanks for inspiring this MLA improvement.
Hi David,
First of all, thank you so much for all the development here. Fantastic service.
I will be donating to support this type of activity and hope others do the same – you are truly unique in your support offerings here.
I got everything operational with your great instructions here. I am seeing each post listed as you said would occur. However, I am not seeing ACF_CHECKBOX_ENABLED. And to be frank, I’m not even sure what the field would represent. (image here)
That said, I would like to have a field or really just a filter that checks if it is used or not in a custom field. Some of these are used in hundreds of pages, so the list gets huge and slow. Any ideas on how I would add a filter to only show image with or without that Header Image field having a value? Or, how to make the field sortable/filterable using Admin Columns Pro?
One more thing – in the way you built this, the fields are added automatically. Would I no longer need to add those fields in the ACF fields part of the plugin?
Thank you David!
-
This reply was modified 6 years ago by
maven1129.
Thanks for the good news regarding your progress and for the kind words. Thanks as well for your intention to donate. I am no longer accepting donations for MLA, but You are welcome to donate to any COVID-19 charitable cause with my gratitude.
Regarding “ACF_CHECKBOX_ENABLED”, you can ignore it. It will only appear if there is an existing custom field with the name/slug value of
acf_checkbox. This feature was created for an earlier support topic and does not apply to your application. If you tire of seeing the “Photo Alt” column on the Media/Assistant admin submenu you can edit the code and change the ACF_REPEATER_ENABLED constant to “false”.You wrote “I would like to have a field or really just a filter that checks if it is used or not in a custom field. Some of these are used in hundreds of pages, so the list gets huge and slow.” I understand that to mean that some images are used in many posts and so the value in the “Header Image” column becomes quite large; is that right? If so, what you want is some kind of simple “yes/no” indicator, or perhaps a count of the number of posts an image is used in; is that right? Then, you want to be able to sort or filter on the value in that new field.
Sorting and filtering on this new column would be straightforward if it was stored in the database as, for example, a custom field. Computing it for each Media/Assistant page refresh would be complicated and time/memory consuming. Dynamically updating it each time a post/page is edited would also be somewhat of a challenge.
I believe it’s feasible to enhance the example plugin to provide an MLA data source that returns the post/page count (if greater than zero) for each item. You could use that to create and populate a standard custom field using MLA’s existing Custom Field mapping rules and make the field a sortable column in the Media/Assistant submenu table. You could also define a custom table view to filter on the presence or absence of a field value. And, of course, the custom field would be available to Admin Columns Pro as well. The only limitation is that you would have to rerun the MLA mapping rule to update the field content after posts/pages have been edited. Is this an acceptable solution for your application?
Finally, you wrote “One more thing – in the way you built this, the fields are added automatically. Would I no longer need to add those fields in the ACF fields part of the plugin?” The “fields” added by the MLA example plugin are just a reporting convenience. To actually assign and maintain field values in your posts/pages requires the ACF plugin, and so you must define them as Advanced Custom Fields.
Let me know what you think about the sort/filter solution I have outlined.
I have uploaded a new MLA Development Version dated 20200514 that includes an updated “MLA Advanced Custom Fields Example” plugin, v1.05. You can install the update by following the instructions in my earlier post to this topic.
In this updated version, Advanced Custom Fields “image” custom field(s) are made available as custom data substitution parameters (data sources), using the prefix “acf:”, e.g., “acf:search_bar_image”. Three format/option values can be added: 1) “acf:search_bar_image(count)” returns the number of item references, 2) “acf:search_bar_image(present)” returns 1 if there are references present, and 3) a numeric value, e.g., “acf:search_bar_image(3)” returns the count only if it is equal to or greater than the number of references.
Once you have installed the updated example plugin you can use MLA’s Custom Field mapping rules to create a custom field for the new data source and add that as a table column you can sort on. To do that:
- Navigate to the Settings/Media Library Assistant “Custom Fields” tab.
- Scroll down to the “Add New Custom Field Rule” area below the “Enable” checkboxes.
- Under the “Name” dropdown control, click “Enter new field” to create a new custom field.
- In the “Name” text box, enter something like “Header Image Count”.
- From the Data Source dropdown list, select “– Template (see below) –”.
- In the “Meta/Template” text box, enter “([+acf:search_bar_image(count)+])”.
- Click the “MLA Column” check box to make the field available in the Media/Assistant submenu table.
- In the “Existing Text” dropdown list, select “Replace”.
- In the “Format” dropdown list, select “Native”.
- In the “Option:” dropdown list, select “Text”.
- Check the “Delete NULL Values” checkbox. This will create the element only for items that have one or more references.
- Leave the “Status” set to “Active”.
- Click the “Add Rule” button to save your work.
Once that’s done you can click the “Execute” rollover action to run the rule for all items. Then, go back to the Media/Assistant page, add the new column if it’s not already displayed and click on the column title to sort. If you sort in descending order the items with references will come to the front of the table.
If you create the custom field, you can define a custom table view to filter the display, showing only the items having one or more references:
- Navigate to the Settings/Media Library Assistant “Views” tab.
- Scroll down to the “Add New View” section on the left-hand side of the page.
- Enter “has-header-image” in the “Slug” text box.
- Enter something like “Has Header Image(s)” in the “Singular Label” and “Plural Label” text boxes.
- Uncheck the “Post MIME Type” box.
- Check the “Table View” box to add the View to the Media/Assistant submenu.
- Enter “0” in the Menu Order text box.
- Enter a Description if you want one.
- Scroll to the bottom of the screen and click “Add View” to save your settings.
- Navigate to the Media/Assistant submenu.
- Click “Has Header Image(s)” in the list of views under the screen title to filter the table.
The new view will be added to the top of the Media/Assistant admin submenu, after the built-in views such as “All”, “Images” and “Unattached”.
As I wrote earlier, the only shortcoming to this approach is that you will have to go back to the rule and click the “Execute” rollover action to manually refresh the values when posts/pages are updated.
It would be great if you can install the Development Version and updated example plugin and let me know how they work for you. Thanks for inspiring these MLA improvements.
It has been a month since my last post in this topic. I hope you have found the solutions you need for your application.
I am marking this topic resolved, but please update it if you have further questions about where-used reporting for ACF Image variables.
-
This reply was modified 6 years ago by
The topic ‘finding “where used” in custom field’ is closed to new replies.