How to Modify Grid Gallery Items Before the Gallery Is Rendered on the Page


Wonder Grid Gallery 18.4 or above


You can use the Wonder Grid Gallery plugin to create a WordPress posts gallery or a custom posts gallery. This plugin allows you to add multiple categories or custom post types to a single gallery.

By default, the gallery displays the posts one category by one category or one custom post type by one custom post type, according to the order in which you have added them to the gallery.

In some instances, you may want to mix all posts or custom post types in a specific order. For example, you may want to display them alphabetically by post title.

The plugin provides a filter named wonderplugin_gridgallery_modify_gallery_items that you can use to modify the array of gallery item objects before the gallery is rendered on the page.

To implement this, navigate in your WordPress dashboard to the left menu, select Appearance -> Theme File Editor. On the right side of the page, find the Theme Functions (functions.php) from the Theme Files list and click to edit it.

Add the following code to the end of the functions.php file to sort all gallery items alphabetically in ascending order:

add_filter( 'wonderplugin_gridgallery_modify_gallery_items', function( $items ) {
	usort($items, function($a, $b)
		if (isset($a->title) && isset($b->title)) {
			return strcmp($a->title, $b->title);
		} else {
			return 0;
  	return $items;
}, 10, 3 );