Hello. Thank you for contacting the Toolset support.
Well - to order parent post by number of child post count, you need to add some custom code.
Could you please tell me your child post type name?
Basically, you should create a Types numeric filed namely "child-count" for your parent post type which will hold the count of your child posts and save the total count while add/delete your child posts.
For example - Please add following code to your current theme's functions.php file:
add_action( 'before_delete_post', 'count_total_children_delete' );
function count_total_children_delete($post_id)
{
if ( get_post_type( $post_id ) == 'child-post-type-slug' ) {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_{parent-post-slug}_id', true );
if($parent_id != '')
{
$childargs = array(
'post_type' => 'child-post-type-slug',
'numberposts' => -1,
'meta_key' => '_wpcf_belongs_{parent-post-slug}_id',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_{parent-post-slug}_id', 'value' => $parent_id))
);
$totalChildCount = count(get_posts($childargs));
update_post_meta( $parent_id, 'wpcf-child-count', $totalChildCount);
}
}
}
add_action( 'save_post', 'count_total_children',20,2);
function count_total_children($post_id){
if ( get_post_type( $post_id ) == 'child-post-type-slug' ) {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_{parent-post-slug}_id', true );
if($parent_id != '')
{
$childargs = array(
'post_type' => 'child-post-type-slug',
'numberposts' => -1,
'meta_key' => '_wpcf_belongs_{parent-post-slug}_id',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_{parent-post-slug}_id', 'value' => $parent_id))
);
$totalChildCount = count(get_posts($childargs));
$oldChildCount=get_post_meta( $parent_id, 'wpcf-child-count', true );
if( $totalChildCount != $oldChildCount ){
update_post_meta( $parent_id, 'wpcf-child-count', $totalChildCount );
}
}
}
Where:
- Replace 'child-post-type-slug' with your original child post type slug
- Replace '{parent-post-slug}' with your original parent post type slug.
Now,
create a view listing your parent custom post type posts, order it by the custom field "child-count" (descending) and list your parent posts.
More info:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/