Searching on custom fields
-
Hi,
First, thanks for the great plugin you developed as we are currently testing it and it looks to provide almost all of the functionality we are looking for.
I searched through all of the support tickets but did not see a question posed on the ability to search from the Assistant screen in custom fields.
For example, we are looking to add a custom field called photo reference where the user would put in the reference number from a stock photo site. It would be helpful for them to be able to search on that custom field to see if they used that particular image in any other article.
Is there a way to add custom fields to the search?
Thanks for your help in this.
https://www.ads-software.com/plugins/media-library-assistant/
-
Thank you for the positive feedback and for your interesting question. The MLA “Keyword Search” function is a minor extension of the traditional WordPress keyword search. Both of them are designed to search fields in the “Posts” database table. Searching on custom field content requires a different database table, “Postmeta”.
You wrote “the user would put in the reference number” and asked “Is there a way to add custom fields to the search?“. Let me make sure I understand the details of your application.
- Your first comment implies that you want this function in the “front end” of your site, not in the “back end” Admin screens such as the Media/Assistant submenu; is that right?
- Your question says “add custom fields“. Do you want to add the custom field(s) to the existing keyword search or do you want a separate, specific, “reference number” search?
The general answer to your question is straightforward. You can use the hooks built in to MLA to intercept the database query used for Keyword Search and modify it or completely replace it. The specific hooks and code required will be different depending on the answers to the above questions.
If you can give me some additional information and let me know if you are prepared for a solution that requires some PHP coding I can give you more specific guidance. Thanks for your interest in the plugin.
Thanks for getting back to me.
1. Actually, I am looking to have this function in the “back end” admin screen
2. It would work for us to add custom fields to the existing keyword search in the back end
A solution that requires some PHP coding is definitely manageable
Thanks so much
Thanks for your response with the additional details. I will do some work on a solution and post my progress here.
I understand you want to add a custom field, “photo reference” to your image items and then search on the content of that field with the Media/Assistant “Search Media” text box. Searching the field would be in addition to the other possible locations such as Title and Description. Is that right?
One further question occurs to me – is the “photo reference” numeric, or does it contain letters and/or punctuation characters. When given a numeric value, the “Search Media” box will do some additional searching to match the number to a Media Library item ID or a parent post/page ID. That might give you some false positive matches. I don’t see that as a big issue, but you should be aware of it.
Further investigation and thought made me realize that implementing “photo reference” as an addition to the existing search parameters would be quite difficult two reasons:
- The existing search parameters include the current view (All, Image, etc.) and all of the filter controls on the Media/Assistant submenu screen, such as the month/year and taxonomy term dropdowns.
- The database query is performed by the WordPress
WP_Query
class. Adding a custom field search to the query with an “OR” connector would require complex modifications in the underlying SQL clauses generated by theWP_Query
logic.
Given your description of the application I hope a simpler alternative will prove useful. I have developed a small, simple custom plugin that allows a custom field search instead of a keyword search. The custom plugin looks for a
custom:
prefix in the Search Media text box and, if found, substitutes a custom field search for the keyword search.The custom plugin lets you search on any of your custom fields and allows for a default custom field name to simplify data entry. It also handles the MLA-specific practice of padding some field values with leading spaces to make them sort reasonably.
I have used “File Size” as my default custom field to show the handling of padded fields. You can simply replace
$field = 'File Size';
with$field = 'photo reference';
(or whatever the field name is) for your application.For your application you would enter this value in the Search Media text box:
custom:photo reference=123456
Or, if you have replaced the default field name you could enter:
custom:123456
I hope you will consider this simpler alternative to my original proposal. I have created mla-custom-field-search-example.php.txt and I will add it to the /examples/ directory in my next MLA version. I have also included the full text of the plugin below so you can use it immediately. All of the code you need can be put in one place; the
mla_list_table_new_instance()
function. This function is called once, just before the Media/Assistant submenu table is crated; it’s an easy place to modify the arguments for the table. Here is the complete source code:<?php /** * Extends the Media/Assistant "Search Media" box to custom field values * * @package MLA Custom Field Search Example * @version 1.00 */ /* Plugin Name: MLA Custom Field Search Example Plugin URI: https://fairtradejudaica.org/media-library-assistant-a-wordpress-plugin/ Description: Extends the Media/Assistant "Search Media" box to custom field values Author: David Lingren Version: 1.00 Author URI: https://fairtradejudaica.org/our-story/staff/ Copyright 2014 - 2015 David Lingren This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You can get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA */ /** * Class MLA Custom Field Search Example hooks one of the filters provided by the MLA_List_Table class * * Call it anything you want, but give it an unlikely and hopefully unique name. Hiding everything * else inside a class means this is the only name you have to worry about. * * @package MLA Custom Field Search Example * @since 1.00 */ class MLACustomFieldSearchExample { /** * Initialization function, similar to __construct() * * @since 1.00 * * @return void */ public static function initialize() { // The filters are only useful for the admin section; exit in the front-end posts/pages if ( ! is_admin() ) return; // Defined in /media-library-assistant/includes/class-mla-main.php add_filter( 'mla_list_table_new_instance', 'MLACustomFieldSearchExample::mla_list_table_new_instance', 10, 1 ); } /** * Extend the MLA_List_Table class * * This filter gives you an opportunity to extend the MLA_List_Table class. * You can also use this filter to inspect or modify any of the $_REQUEST arguments. * * @since 1.00 * * @param object $mla_list_table NULL, to indicate no extension/use the base class. * * @return object updated mla_list_table object. */ public static function mla_list_table_new_instance( $mla_list_table ) { // Look for the special "custom:" prefix in the Search Media text box if ( isset( $_REQUEST['s'] ) && ( 'custom:' == substr( $_REQUEST['s'], 0, 7 ) ) ) { $tokens = explode( '=', substr( $_REQUEST['s'], 7 ) ) ; // See if the custom field name is present, followed by "=" and a value if ( 1 < count( $tokens ) ) { $field = array_shift( $tokens ); $value = implode( '=', $tokens ); } else { // Supply a default custom field name $field = 'File Size'; $value = $tokens[0]; } // Numeric and "commas" format fields are often padded to sort reasonably if ( in_array( $field, array( 'File Size', 'pixels', 'width', 'height' ) ) ) { $value = str_pad( $value, 15, ' ', STR_PAD_LEFT ); } $_REQUEST['mla-metakey'] = $field; $_REQUEST['mla-metavalue'] = $value; unset( $_REQUEST['s'] ); unset( $_REQUEST['mla_search_connector'] ); unset( $_REQUEST['mla_search_fields'] ); } // isset s=custom: return $mla_list_table; } // mla_list_table_new_instance } // Class MLACustomFieldSearchExample /* * Install the filters at an early opportunity */ add_action('init', 'MLACustomFieldSearchExample::initialize'); ?>
If you need more specific guidance on copying the code to a PHP file and uploading it as a new plugin, let me know. I will leave this topic unresolved until I hear back from you. Thanks for your patience.
I have just uploaded a new Development Version that contains the mla-custom-field-search-example.php.txt custom plugin.
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 example 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.
I have successfully installed and tried the Development Version with the new plugin.
It is working quite well when doing exact searches on the custom field. I tried to put in a partial match of a value that is in the custom field and nothing came up, however.
Does the mla-custom-field-search-example allow for partial matches and, if so, is there a character I need to add to enable that?
Thanks for your responsiveness and great work (again) on this plugin
Thanks for trying the new custom plugin and for your report with the good news.
The current implementation does not support partial matches; it is based on some existing custom field filtering code that requires an exact match. I will have a look at the code and see if I can extend it to partial matching without any adverse consequences. I will post an update here when I complete the investigation.
If you would like any assistance with your investigation, we have a developer in our organization that has done work with plugins and would be happy to help out. Just let me know.
Thanks for your patience and for your offer to help out with the plugin enhancements. I was able to make some progress on your topic in the context of other work I am doing for my next release.
I have just uploaded a new Development Version dated 20150609 that contains an extensive revision to the
mla-custom-field-search-example.php.txt
custom plugin. The revised example takes advantage of some new Media/Assistant submenu hooks that allow more flexibility in modifying the query used to select Media Library items for display. It also uses some code from the “Library Views/Post MIME Type Processing” Table View feature (see the Documentation tab) that supports powerful partial match searches:- To return all items that have a non-NULL value in the field, simply enter the prefix “custom:” followed by the custom field name, for example,
custom:File Size
. You can also enter the custom field name and then “=*”, e.g.,custom:File Size=*
. - To return all items that have a NULL value in the field, enter the custom field name and then “=”, e.g.,
custom:File Size=
.<.li> - To return all items that match one or more values, enter the prefix “custom:” followed by the custom field name and then “=” followed by a list of values. For example,
custom:Color=red
orcustom:Color=red,green,blue
. Wildcard specifications are also supported; for example, “*post” to match anything ending in “post” or “th*da*” to match values like “the date” and “this day”.
I will leave this topic unresolved until the new example is released in the next MLA version. If you get a chance to try the updated Development Version, let me know if it meets your needs. Thanks for inspiring some new MLA features and for your interest in the plugin.
Thanks for working on this. I installed the latest developer version, however, I did not see the results that you described. Here are my findings. I, obviously, did not use the example fields below in my testing; I just included them to match up with your examples
custom:File Size returns no results
custom:File Size=* returns no results
custom:File Size= returns all of the records without a Null value and always one additional record that does not match
custom:Color=red returns the expected results
custom:Color=red,green,blue returns no results
custom:[any valid wildcard] returns no results
If you need any further information, please let me know. Thanks
Thanks for your update and for taking the time to install and test the latest Development Version. I’d like to confirm two parts of the update process:
- After installing the Development Version, did you also install and activate the latest version of the “MLA Custom Field Search Example” custom plugin? It will say version 1.01 on the PLugins/Installed Plugins screen.
- Did you also modify the source code of the custom to replace “File Size” with the name of whatever custom field you want to use as your default? There are three separate occurrences of this literal in the source code.
I would also be interested in the “always one additional record that does not match“; can you say any more about that?
Your first example, “custom:File Size”, will search your default custom field for a value of “File Size” – probably not finding it.
If you have installed both the 20150609 Development version and the modified version 1.01 custom plugin I will have to investigate further. I can send you a copy of the custom plugin that writes debug information to the error log. You can use the instructions in the “MLA Debug Tab” section of the Settings/Media Library Assistant Documentation tab to view the information and send a copy back to me.
We could also do a Skype or Google Hangout session and you could share your screen with me so I can see what’s going on.
I regret the trouble you’re having and I want to get this working for you. Let me know how I can best help.
Further update
1. I had not installed the latest version of the “MLA Custom Field Search Example” custom plugin. Previously I put this in the mu-plugins folder and so I replaced the previous file there with the new one
2. I did not modify this yet but in all my testing I am always referencing a specific custom field after “custom:”
After uploading the latest version of the “MLA Custom Field Search Example” custom plugin there is definitely more success although still a few issues. I have put the remaining issues in bold below. Again, in my testing I am using the names of my custom fields but have put File Size below to keep consistency
custom:File Size still returns no results
custom:File Size=* now returns the expected results
custom:File Size= returns all records including those without a Null value
custom:Color=red returns the expected results
custom:Color=red,green,blue now returns the expected results
custom:[any valid wildcard] still returns no results
I can definitely install the custom debug plugin and send the results or get on a Skype session with you.
Thanks so much for all of your work and willingness to help
Thanks for your “further update” and for re-testing the new version. I have found and fixed some defects in the code and I am embarrassed that my own testing did not reveal them earlier.
I also added some code to activate the
error_log
debugging, but it may not be needed if my fixes work for you. Specifically:- custom:File Size still returns no results
Yes, that is correct. This case searches the default custom field (File Size) for the literal value “File Size” and doesn’t find it. - custom:File Size=
This should now return only the items with NULL values. - custom:[any valid wildcard]
Without a field name and ‘=’ this will search the default custom field (File Size) for the wildcard and may not find anything. If, however, you enter something likeColor=*e*
it should give you every item with an “e” in the value.
The NULL values and “any valid wildcard” cases were broken in my first version and should be fixed in the latest version. I have uploaded the latest version (1.02) to the /examples directory in the Development Version dates 20150611. You don’t need the new Development Version, but it would be great if you updated to it just to give it a workout.
Thanks for your patience and persistence.
I installed the latest version (1.02) and also updated to the latest Development Version
Here are my results
1. custom:File Size
– Thanks for your explanation as I wasn’t clear on how this worked previously. I did replace “File Size” in the source code with one of my custom fields “Source” for testing. “istock” is a value for one record in the “Source” field but when I entered “custom:istock” I did not get any results2. custom:File Size=
-An example will best explain this:
* In addition to the “Source” field mentioned above, I also have a custom field called “External ID”. If I enter in “custom:Source=”, the results will not show any of the records that have a value in “Source” which is expected. However, the results also will not show any of the records where there is a value in “External ID” but no value in “Source”. Perhaps when a record is edited and updated the NULL value in “Source” becomes “” ?3. custom:[any valid wildcard]
– Works great now!If you need error log just let me know how to access that and I can send it to you
Thanks you so much again for your work. I’m fell bad that this continues to have issues
Thanks for installing the updates and for reporting your results. Don’t feel bad about the continuing adventures!
Regarding “1. custom:File Size“, make sure you changed “File Size” to “Source” in both of the first two source code occurrences. have you tried entering “custom:*” to see which items are displayed, or “custom:Source=istock” or “custom:*istock*”? You can also go to the Media/Edit Media screen and verify that no leading or trailing spaces are in the value, etc.
Regarding “2. custom:File Size=“, your comment about updating the record gave me a clue. Did you populate the Source field with an IPTC/EXIF mapping rule? I did some testing and found that the MLA IPTC/EXIF mapping rules will put an empty value ( “” )in custom fields if the source data isn’t in the image. I did not invent the “Delete NULL values” option until I developed the Custom Field mapping feature in a later MLA version.
If that’s the problem you can fix it by deleting your IPTC/EXIF mapping rule and replacing it with a Custom Field mapping rule. For example, if you have an EXIF field called “Artist” you can select “– Template (see below) –” as the custom field Data Source, enter “([+exif:Artist+])” in the text box below and check the “Delete NULL values” box. When you “Map All Attachments” you should get the results you need. I regret the oversight and I will add a “Delete NULL values” option to the IPTC/EXIF rules in a future MLA version.
If you’re still having trouble with your first “istock” issue you can follow the instructions in the “MLA Debug Tab” section of the Settings/Media Library Assistant Documentation tab to activate the Debug tab and see the error log file. To write debug information to the file, add “<|>” to the beginning of your search value, e.g., “<|>custom:istock”. Run the search, then go to the Debug tab and view the results. You can copy and paste the content of the Error Log window or click “Download” to get a text file with the content. Click “Reset” to empty the file and start over.
If you want to e-mail an error log to me, give me your contact information and I will respond with an address you can use to send me any information you think necessary. You can use the Contact Us page at the FTJ web site:
Do not post your e-mail address in the forum; personal details in a public forum violates WordPress guidelines. Thanks for your persistence in working with me on this interesting custom plugin example.
- The topic ‘Searching on custom fields’ is closed to new replies.