Hello. Thank you for contacting the Toolset support.
Well - to order parent post by number of count of it's associated child posts 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 "Game" which will hold the count of your child posts and save the total count while add/delete your child posts.
For example:
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_game_id', true );
if($parent_id != '')
{
$childargs = array(
'post_type' => 'child-post-type-slug',
'numberposts' => -1,
'meta_key' => '_wpcf_belongs_game_id',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_game_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_game_id', true );
if($parent_id != '')
{
$childargs = array(
'post_type' => 'child-post-type-slug',
'numberposts' => -1,
'meta_key' => '_wpcf_belongs_game_id',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_game_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
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/