Skip Navigation

[Resolved] How to order parent posts according to their child post count?

This support ticket is created 7 years, 1 month 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by Minesh 7 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#577288

Hello

I have a parent post called "Game" which has many children posts.

How to order "Game" posts by their child post count? I want the parent posts with more children to be at the top of the list.

Thnaks

#577316

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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/

#578272

Hello

I have five associated child posts:

"sites-rating"
"manshor"
"breaking-news"
"user-post"
"user-review"

I have created a numeric filed called "child-count" for my parent post type "Game".

Can you tell me how should the function will be now?

Thanks for your time.

#578438

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - as I understand CPT game have been assigned with 5 child post types .

Now, question is, you want to have total child count irrespective of any child post type or the count should be setup as particular child count belongs to each child post type?

#578756

Hello

> you want to have total child count irrespective of any child post type ?

Yes.

#579071

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - to count the total child irrespective of the post type its attached to could you please try the following code and try to resolve your issue.

add_action( 'save_post', 'count_total_children',20,2);
function count_total_children($post_id){
$post_type_array = array("sites-rating","manshor","breaking-news","user-post","user-review");

    if (in_array(get_post_type( $post_id ),$post_type_array) ) {
        $parent_id = get_post_meta( $post_id, '_wpcf_belongs_game_id', true );
        if($parent_id != ''){
            $totalChildCount = get_post_type_count($parent_id );
            $oldChildCount=get_post_meta( $parent_id, 'wpcf-child-count', true );
            if( $totalChildCount != $oldChildCount ){
                update_post_meta( $parent_id, 'wpcf-child-count', $totalChildCount );
            }
        }
    }
}

function get_post_type_count($parent_id){
$post_type_array = array("sites-rating","manshor","breaking-news","user-post","user-review");

$count_childs = array();
foreach($post_type_array as $k=>$v):

$childargs = array(
                'post_type' => $v,
                '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))
            );
           $count_childs[]  = count(get_posts($childargs));
endforeach;

       return array_sum($count_childs);     
  
}

#579289

Hello

I added the code to functions.php but I got an error:

Parse error: syntax error, unexpected end of file in /home/site-user/public_html/wp-content/themes/mytheme/functions.php on line 668

#579291

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#579399

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - there was a issue with the closing bracket that I've fixed.

Could you please check now and try to resolve your issue.

#579429

Looks good.

How can I update the count for all old games?

Thanks

#579449

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - There is no automatic way to do that. you need to build and run a script or you need to save any child post type having different game post at least once.