Skip to content

Total Theme: Get Images from Media Library that have a featured custom_meta


//https://total.wpexplorer.com/docs/snippets/attachments-gallery-slider/
add_filter( 'wpex_get_post_gallery_ids', function( $ids ) {
	/*
	$images = get_attached_media( 'image' );
	if ( $images ) {
		$ids = array(); // reset incase there are any defined gallery images lets not include them
		foreach ( $images as $image ) {
			$ids[] = $image->ID;
		}
	}
	//$ids = array(393,339);
	//*/
	//return get_featured_media_ids_from_library();//$ids;
	//
	
	// Prepare an array to store the media IDs.
    
/*
    // Query the media library to retrieve media items with 'enym-featured' custom field set to 1.
    $args = array(
        'post_type'      => 'attachment',
        'post_mime_type' => 'image', // You can change this to the desired media type, e.g., 'video', 'audio', etc.
        'posts_per_page' => -1,
        'fields'         => 'ids', // Fetch only the IDs.
        'meta_query'     => array(
            array(
                'key'     => 'enym-featured', // Custom field name
                'value'   => '1', // Custom field value to filter by
                'compare' => '=', // Use '=' for exact match
            ),
        ),
    );

    $media_query = new WP_Query($args);
	
	print_r($media_query);

    if ($media_query->have_posts()) {
        // Loop through the results and add IDs to the array.
        while ($media_query->have_posts()) {
            $media_query->the_post();
            $media_ids[] = get_the_ID();
        }
        // Reset post data to avoid conflicts with other queries.
        wp_reset_postdata();
    }

    return array(393,339); //$media_ids;
*/
	
	//$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image');

	$media_ids = array();
	$args = array(
		'post_type'      => 'attachment',
		'post_mime_type' => 'image', // You can change this to the desired media type, e.g., 'video', 'audio', etc.
		'posts_per_page' => -1,
		'meta_query'     => array(
			array(
				'key'     => 'enym-featured', // Custom field name
				'value'   => '1', // Custom field value to filter by
				'compare' => '=', // Use '=' for exact match
			),
		),
    );

	$attachments = get_posts($args);
	
	if ($attachments) {
		foreach ( $attachments as $attachment ) { 
			$media_ids[] = $attachment->ID;
		}
	}
	
	return $media_ids;
	
} );

Dieser Beitrag hat 0 Kommentare

Schreibe einen Kommentar

Deine E-Mail wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

An den Anfang scrollen