Skip Navigation

[Closed] Order posts by the number of childrem

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.

This topic contains 2 replies, has 2 voices.

Last updated by Mohammed 6 years, 1 month ago.

Assigned support staff: Mohammed.

Author
Posts
#332022

I am trying to create a list of a Type but ordered by the number of children that it has. I have an Course type that has a child for each instance of the course. I want to list the TOP 5 courses, based on the number of times that it has been run.

Additionally I want to create a "Popular" list as well, with a list of Courses ordered by the most recent time that they have been run!

How might this be achieved (I am not a PHP guy)...

#332138

Hello Martin,

Thank you for contacting Toolset support forum,

You have to parts in your question, The first one is that you want to order the courses by the number of its children.

Please follow these steps in order to make them sorted.
- What will happen is that we will create a custom field for the parent courses which will be incremented by custom code while creating a child post and then we will order the posts according to this field.

Steps:
- After creating the two custom post types and making sure that you set the relationship between them,
- Create a custom field and give it the name “course children count” and the slug “course-children-count” .
- Assign this field to the parent post type
– Go to Types > Custom Fields > Where to display this group section and select the Courses post type.

- Using an FTP program, Add the following code to your current theme functions.php file
– You can find this file under /website-root-directory/wp-content/themes/your-theme-name/
---- But you have to change the values in this code to be adapted with you configurations
---- The values to be changes

*** course-run => will be replaced with the slug of your child post type
*** _wpcf_belongs_course_id => The word course here will be replaced with your parent custom post type slug

function recount_my_totals($post_id){
	if ( get_post_type( $post_id ) == 'course-run' ) {
	global $wpdb;
		$parent_id = get_post_meta( $post_id, '_wpcf_belongs_course_id', true );
	
		$totalPostsCount = $wpdb->get_var("select count(meta_value) from $wpdb->posts p join $wpdb->postmeta pm on post_id=p.ID and meta_key = '_wpcf_belongs_course_id' where pm.meta_value = '$parent_id'");
		$oldCount=get_post_meta( $parent_id, 'wpcf-course-children-count', true );
		if( $totalPostsCount>$oldCount ){
		update_post_meta( $parent_id, 'wpcf-course-children-count', $oldCount+1 );
		}
	}
}

add_action( 'save_post', 'recount_my_totals' );

- Go and edit your view and in the “Ordering” section, select the custom field we created “course children count“ and choose if you want to order Ascending or Descending

- About the second part , How could you determine that the child course is run or still not run?
Is there an action you may take to do that? Or you just change the status of the post ?

Please clear this point for me so that I can suggest a solution .

Thanks and regards.

#333338

Hello Martin,

Please let me know if the provided solution worked for you.

Thanks and regards.

The topic ‘[Closed] Order posts by the number of childrem’ is closed to new replies.