WordPress.org

Ready to get started?Download WordPress

Forums

Attachments
[resolved] Search results based on filenames. (5 posts)

  1. C8H10N4O2
    Member
    Posted 1 year ago #

    Hi.

    First, let me thank you for this wonderfull plugin.

    When a user doesn't put something on the title or description field for an attachment, I display the name of the file instead whenever it's needed.

    For those who are interested, here is the piece of code :
    <?php echo basename($attachments->url()); ?>

    Just wrap it in a if statement to check if $attachments->title() is empty or not.

    I now would like my users to be able to search for files based on the filename. Is this possible with this plugin or with a slight code modification ?

    Here is a picture to explain.

    You can see that by searching for "Lorem", the Lorem Ipsum 2013 doc is displayed, wich is fine because Lorem is in the title of the file. But I would like this doc to be also displayed when searching for a string in the filename, for example : "2013_06" or ".xslx".

    Many thanks.

    http://wordpress.org/extend/plugins/attachments/

  2. Jonathan Christopher
    Member
    Plugin Author

    Posted 1 year ago #

    Attachments doesn't currently store the filename in it's metadata, only the WordPress Media ID and the field values. I'll add it to the feature requests but in the meantime it'd be possible to reverse search based on querying WordPress media entries for the filename and using the returned IDs in an Attachments Search.

  3. C8H10N4O2
    Member
    Posted 1 year ago #

    Thanks for your answer.

    Querying wordpress on the filename of an attachment seems quite difficult since the only field where this information is available is the guid in the wp_post table. I think WP_query can't help here so a custom $wpdb query may be needed.

    Anyway I'm no developper and can't do that so I decided to go with get_posts, which seems to search for the post_title, which is the filename without the extension in the case of an attachment.

    So this way I retrieved a list of attachment_ids but then I have to search for those attachment_ids using $attachments->search().

    But I don't think I can do it with one $attachments->search() only : it seems like only one attachment_id is authorized in the $attachments->search() parameters list. Am I wrong ? (Feature request ?)

    I may succeed by doing a loop on the attachment_ids and as much searchs as ids I have, and then merge all the objects...

    Am I over-complicating things ?

    Maybe there is a workaround, like setting a new field "filename" in my instance, and pre-populate the field with the filename when a user attach a file to a post ? Is this possible (with a hook?) ?

    Many thanks !

  4. Jonathan Christopher
    Member
    Plugin Author

    Posted 1 year ago #

    You're right, you'd need to iterate over all of the IDs when search()ing. Unfortunately the inclusion of a filter wouldn't help much here because Attachments doesn't store the filename in it's metadata, but perhaps it can be included down the line.

  5. C8H10N4O2
    Member
    Posted 1 year ago #

    Thank you very much for your answer.

    I'll stay with a simple $attachments->search() on the name of the attachment for my project.

    I'll post a feature request on Github regarding the possibility to include the filename in Attachment's metadata.

    Again, thanks for this great plugin.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic