• Resolved fff654

    (@fff654)


    Hi,

    Is there a way to have in the gallery all images of the WP Media Library that include, for example, “my_string-” in the filename ?
    PHP is OK …

    Thanks in advance,

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author bradvin

    (@bradvin)

    hi @fff654

    Yes you can do this with some custom PHP code. I have tested this and it works by doing a search for attachment names:

    /**

    * Override attachments for a FooGallery based on a search by attachment filename.

    *

    * example :

    *

    * [foogallery id="123" attachment_search="my_string-"]

    */

    add_filter( 'foogallery_render_template_argument_overrides', function( $foogallery, $args ) {

    if ( empty( $args['attachment_search'] ) ) {

    return $foogallery;

    }

    $search = sanitize_file_name( wp_unslash( $args['attachment_search'] ) );

    if ( '' === $search ) {

    return $foogallery;

    }

    global $wpdb;

    // _wp_attached_file values are usually like "2026/03/my_string-image.jpg"

    $ids = get_posts( array(

    'post_type' => 'attachment',

    'post_status' => 'inherit',

    'post_mime_type' => 'image',

    'fields' => 'ids',

    'numberposts' => -1,

    'orderby' => 'date',

    'order' => 'ASC',

    'update_post_meta_cache' => false,

    'update_post_term_cache' => false,

    'meta_query' => array(

    array(

    'key' => '_wp_attached_file',

    'value' => $search,

    'compare' => 'LIKE',

    )

    ),

    ) );

    $foogallery->attachment_ids = array_map( 'intval', $ids );

    return $foogallery;

    }, 10, 2 );
    Thread Starter fff654

    (@fff654)

    super great !

    Thanks Brad

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.