Skip Navigation

[Resolved] How to order parent posts by the count of child posts?

This thread is resolved. Here is a description of the problem and solution.

Problem:
An earlier solution built on Types 2 stopped working after migrating to Types 3. How is it now possible to order a View of parent posts by the count of child posts related to each parent?

Solution:
This requires writing custom code that modifies the main View query, an example of which is proposed below: https://toolset.com/forums/topic/after-relationships-update-code-views-ordering-is-not-working/#post-1177723

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 5 years, 11 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.

Our next available supporter will start replying to tickets in about 0.05 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 9 replies, has 2 voices.

Last updated by simranjeetS-2 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1176807

I am trying to: do this - https://toolset.com/forums/topic/ordering-in-views-parent-posts-with-number-of-child-posts-descending/
The code on that page was working before. Now it is not working. see this : hidden link

sorting on basis of number of cred comments is not working. After "relationships" update, code for views ordering is not working. How to make it work?

Link to a page where the issue can be seen: hidden link

I expected to see: proper sorting

Instead, I got: incorrect sorting as per latest updated

#1176881

Nigel
Supporter

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

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

Hi there

I looked at the thread you linked to, where Minesh provided some custom code.

Am I to understand that the change you have made which has caused this to stop working is to run the relationships migration, is that correct?

I tried to look at your site to see for myself, but the admin pages lead to a 404 error, I'm guessing you have a security plugin installed that prevents me from attempting to log in.

Let me mark your next reply as private so you can update the directions to access the admin area of your site...

#1176888
#1176889

Am I to understand that the change you have made which has caused this to stop working is to run the relationships migration, is that correct?

YES! exactly

#1176989

Nigel
Supporter

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

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

Sorry, I'm slightly confused, because in the linked thread Minesh created code which is to order a parent post type lawyer by a child post type 'cred-comment', but looking at your site I see no such post type, and the relationship is between lawyer and lawyer-review post types.

Did you update your site and change the child post type from cred-comment to lawyer-review?

If that's the case you may be able to get the custom code working again by update the post type slugs.

Did you try that already?

#1177408

I will check that part and update you. But even my custom pic size is not working. I got that by code too. Does all this have anything to do with "belong" part of the code? Anyways, let me update you first.

#1177449

Just checked. it is lawyer-review only.
not working. none of the functions file edits are working after migration

#1177566

Nigel
Supporter

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

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

The problems with the custom code arising from the migration are likely linked to the fact that the relationship is no longer stored as post meta (e.g. '_wpcf_belongs_parent-slud_id') but in dedicated tables, instead.

The migration was designed to catch many common use cases of custom code and handle them automatically, but it looks like that might not have worked in your case for some reason.

In which case any code that is based upon referencing related posts based upon the _wpcf_belongs... post meta will need updating to use the current version of the relationships API.

toolset_get_related_post is the most direct comparison, which can be used to get the id of a parent for the current child post (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post).

For more complex relationships (where there may be more than one result, e.g. when getting child posts of a parent) you would use toolset_get_related_posts (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts).

For the code specific to this thread, namely ordering parent posts by the count of child posts, I would propose a different solution than that used in the earlier thread in any case, so let me suggest some alternative code that you could use.

Try adding the following. (Now in Types you can add code snippets at Toolset > Settings > Custom Code so that you don't need to edit your theme's functions.php file.)

/**
 * Order parent posts by child post count
 */
function tssupp_filter_queried($query, $view_settings, $view_id) {

	$relationship_slug = 'lawyer_lawyer_review'; // Edit as required

	if (in_array($view_id, array(855))) { // Edit ID(s) of View to manipulate

		$results = $query->posts;

		if (count($results) > 0) {

			foreach ($results as $key => $result) {

				$children = toolset_get_related_posts($result->ID, $relationship_slug, array('query_by_role' => 'parent', 'role_to_return' => 'child', 'need_found_rows' => true));

				$result->child_count = $children['found_rows'];
			}

			usort($results, "custom_cmp");

			$query->posts = $results;
		}
	}
	return $query;
}
add_filter('wpv_filter_query_post_process', 'tssupp_filter_queried', 101, 3);

function custom_cmp($a, $b) {
	if ($a->child_count == $b->child_count) {
		return 0;
	}
	return ($a->child_count > $b->child_count) ? -1 : 1;
}

Let me know if that works.

#1177658

the following code did not work

<?php
/**
* New custom code snippet.
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

/**
* Order parent posts by child post count
*/
function tssupp_filter_queried($query, $view_settings, $view_id) {

$relationship_slug = 'lawyer-review'; // Edit as required

if (in_array($view_id, array(855))) { // Edit ID(s) of View to manipulate

$results = $query->posts;

if (count($results) > 0) {

foreach ($results as $key => $result) {

$children = toolset_get_related_posts($result->ID, $relationship_slug, array('query_by_role' => 'parent', 'role_to_return' => 'child', 'need_found_rows' => true));

$result->child_count = $children['found_rows'];
}

usort($results, "custom_cmp");

$query->posts = $results;
}
}
return $query;
}
add_filter('wpv_filter_query_post_process', 'tssupp_filter_queried', 101, 3);

function custom_cmp($a, $b) {
if ($a->child_count == $b->child_count) {
return 0;
}
return ($a->child_count > $b->child_count) ? -1 : 1;
}

#1177723

Nigel
Supporter

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

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

Sorry, the relationship slug in the sample code needed editing to match that on your site.

But, while looking at this it occurred to me that the way I implemented it would be affected by pagination, so I've amended the code to use the wpv_filter_query filter, as follows:

toolset_snippet_security_check() or die('Direct access is not allowed');

/**
 * Order parent posts by child post count
 */
function tssupp_filter_query($view_args, $view_settings, $view_id) {

	$parent_slug = 'lawyer';
	$child_slug = 'lawyer-review';
	$relationship_slug = 'lawyer_lawyer-review';

	if (in_array($view_id, array(855))) { // Edit View ID

		$parents = get_posts(array(
			'post_type' => $parent_slug,
			'nopaging' => true,
			'post_status' => 'publish',
			'tax_query' => $view_args['tax_query'],
		));

		if (count($parents) > 0) {

			foreach ($parents as $parent) {

				$children = toolset_get_related_posts($parent->ID, $relationship_slug, array('query_by_role' => 'parent', 'role_to_return' => 'child', 'need_found_rows' => true));
				$parent->child_count = $children['found_rows'];
			}

			usort($parents, "custom_cmp");

			$parent_ids = wp_list_pluck($parents, 'ID');

			$view_args['post__in'] = $parent_ids;
			$view_args['post__not_in'] = null;
			$view_args['orderby'] = 'post__in';
		}
	}
	return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

function custom_cmp($a, $b) {
	if ($a->child_count == $b->child_count) {
		return 0;
	}
	return ($a->child_count > $b->child_count) ? -1 : 1;
}

I tested it on your site and it is working.

#1177728

My issue is resolved now. Thank you!