Skip Navigation

[Resolved] Ordering in views – parent posts with number of child posts descending

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

Problem:
How to order parent posts with number of child post count

Solution:
You need to use WordPress standard "save_post" hook in order to update the total number of child counts per parent post and use this custom field for ordering option for your parent.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/ordering-in-views-parent-posts-with-number-of-child-posts-descending/#post-630562

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

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

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)

Author
Posts
#630356

Tell us what you are trying to do? I have a parent post and a child post. I want to order views with number of child posts each parent post has in descending order. This ordering option is not available in default views ordering options. How to use this in functions.php or any other way to do this.

Is there any documentation that you are following? no

Is there a similar example that we can see? No. But examples are - sorting houses by number of rooms, sorting professionals by number of reviews.

What is the link to your site? hidden link

#630465

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 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/

#630551

I did this. Added code. But the field/ ordering option is not visible. Did I give my admin to you? Can you check what the problem is. I added } in the end of code to remove php error

#630555

Minesh
Supporter

Languages: English (English )

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

Have you changed/replaced following in code?
- Replace 'child-post-type-slug' with your original child post type slug
- Replace '{parent-post-slug}' with your original parent post type slug.

Yes, sure. I'm happy to help: Please clarify your parent and child post types and view you are using.

*** 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.

#630562

Minesh
Supporter

Languages: English (English )

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

Ok - I've corrected the code as given under:

add_action( 'before_delete_post', 'count_total_children_delete' );
function count_total_children_delete($post_id)
{
    if ( get_post_type( $post_id ) == 'cred-comment' ) {
        $parent_id = get_post_meta( $post_id, '_wpcf_belongs_lawyer_id', true );
        if($parent_id != '')
        {
            $childargs = array(
                'post_type' => 'cred-comment',
                'numberposts' => -1,
                'meta_key' => '_wpcf_belongs_lawyer_id',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'meta_query' => array(array('key' => '_wpcf_belongs_lawyer_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 ) == 'cred-comment' ) {
        $parent_id = get_post_meta( $post_id, '_wpcf_belongs_lawyer_id', true );
        if($parent_id != '')
        {
            $childargs = array(
                'post_type' => 'cred-comment',
                'numberposts' => -1,
                'meta_key' => '_wpcf_belongs_lawyer_id',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'meta_query' => array(array('key' => '_wpcf_belongs_lawyer_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 );
            }
        }
    }
}	

I've created test page here where I've added the "test" view:
=> hidden link

it's showing correct results - you need to save your current parent post type posts at least once to save the current child posts attached to your parent posts.

#630576

Its not working - I cant see any other lawyer/s except the one with cred-comment/s. I want to see all lawyer/s - with or without cred-comment/s. Lawyer/s with max cred-comment/s should be at the top. Then lawyer/s should appear in descending order of number of cred-comment/s.

#630582

Minesh
Supporter

Languages: English (English )

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

I think you are not following me.

I already added in my previous post:
you need to re-save your ALL parent post type posts at least once to save the current child posts attached to your parent posts.

So - go to your parent post type lawyer - edit each entry and save it and then display the view - it should work.

Also - I've added following code to save_post action hook to assign 0 to child-count field by default:

if ( get_post_type( $post_id ) == 'lawyer' ) {	
	 $count=get_post_meta( $post_id, 'wpcf-child-count', true );
			if($count == ''){
				add_post_meta($post_id, 'wpcf-child-count',0);
			}
 }
#630604

Thanks Minesh. Please close ticket