Skip Navigation

[Resolved] Grouping by custom field

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 4 replies, has 3 voices.

Last updated by briana-5 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#921462

In a view I want to group posts that match a certain date and location (that are custom fields) by the date and location, so that it looks like this.

Location: Library - Date: April 1, 2018
-Post 1
-Post 2
-Post 3

Community Center - April 15, 2018
-Post 1
-Post 2

Library June 15, 208
-Post 1
-Post 2
-Post 3

In the older post linked below, it describes a workaround, but I'm wondering if there is now a more simplified approach to accomplishing the above.

https://toolset.com/forums/topic/how-can-i-group-views-by-custom-field/

#921572

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

An alternative approach may or may not be simpler depending on your level of PHP.

The most direct way is to use the WordPress filter posts_groupby (https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_groupby).

It is not so commonly used or especially well documented, and if the official document above isn't sufficient to understand how to use it I suggest you search around for some tutorials or code examples.

It lets you directly manipulate the GROUP BY clause of the SQL query generated by WP_Query when retrieving posts (e.g. when using a View).

The filter will be triggered for each and every query. As well as the $groupby parameter that you will want to set and return, it also makes the $query object available so you can test that you are adding a $groupby clause for the target query.

It would be good practice to add the filter just before Views constructs the query in question and then remove it immediately afterwards, which you can do with the wpv_filter_query and wpv_filter_query_post_process hooks (hidden link).

You'll need to work out the details for your particular case, but as a starting point your code will want to look something like this:

function tssupp_add_groupby( $view_args, $view_settings, $view_id ){

	if ( $view_id == 99 ) {

		// add posts_groupby filter for this particular View
		add_filter( 'posts_groupby', 'tssupp_groupby', 10, 2 );
	}

	return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_add_groupby', 101, 3);


function tssupp_remove_groupby( $view_args, $view_settings, $view_id ){

	if ( $view_id == 99 ) {

		// remove posts_groupby filter for this View when done
		remove_filter( 'posts_groupby', 'tssupp_groupby', 10, 2 );
	}

	return $view_args;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_remove_groupby', 101, 3);


function tssupp_groupby( $groupby, $query ){

	error_log(print_r($query, true));
	error_log(print_r($groupby, true));

	return $groupby;
}

Note that in the posts_groupby filter I'm simply dumping the arguments to the log so you can see what is available to you.

If this doesn't make much sense you'll need to enlist the help of a developer—or persist with the solution of using multiple Views.

#921903

I think I didn't form my question well. How can I use Toolset to accomplish my goal?

#922622

Hi, Nigel is on holiday this week. It sounds like you want to group the results of a View by the values of two custom fields without using additional code. This type of grouping may be possible in Views with somewhat complex nesting of multiple Views as described in the other ticket, but there isn't a straightforward way to group results of a single View like this. I can see how this type of grouping would be useful, so I invite you to create a new ticket using the "Suggest an improvement" option here in the forum. Describe what you would like to accomplish, and our developers will evaluate your request.

#922809

Thanks.