Skip Navigation

[Resolved] I want to show in a view posts order by specific child posts count.

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

Problem:

Get child post of post reference field using PHP codes.

Solution:

The post reference field is also based on one-to-many relationship, you can follow the document I mentioned above to setup PHP codes using function toolset_get_related_posts().

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 3 years, 10 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by georgeK-5 3 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1656433

Tell us what you are trying to do?
I want to show in a view posts order by specific child posts count.

Is there any documentation that you are following?
No.

Is there a similar example that we can see?
https://toolset.com/forums/topic/ordering-in-views-parent-posts-with-number-of-child-posts-descending/#post-630562

This is the code that I use. But I have this situation when my child post are created, the parent post custom field "sea_service_vessel" is related with Post Reference in child CPT fields. The strange thing is that I can not find the 'meta_key' "_wpcf_sea_service_vessel" for this post.
Parent Post "vessel"
Child Post "seaman-sea-service"

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_sea_service_vessel', true );
        if($parent_id != '')
        {
            $childargs = array(
                'post_type' => 'seaman-sea-service',
                'numberposts' => -1,
                'meta_key' => '_wpcf_sea_service_vessel',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'meta_query' => array(array('key' => '_wpcf_sea_service_vessel', '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_sea_service_vessel', true );
        if($parent_id != '')
        {
            $childargs = array(
                'post_type' => 'cred-comment',
                'numberposts' => -1,
                'meta_key' => '_wpcf_sea_service_vessel',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'meta_query' => array(array('key' => '_wpcf_sea_service_vessel', '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 );
            }
        }
    }
    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);
              }
   }
}   
#1656639

Hello,

The thread you mentioned above is outdated, it is fro legacy post type relationship, in the latest version of Toolset Types plugin, there isn't "_wpcf_sea_service_vessel" any more.

You will need to follow our document to get the related child posts, and count the result:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

#1656855
Correct assosiation.png

This is related to the thread https://toolset.com/forums/topic/filtering-view-with-two-complex-relationships-get_related_posts-error/#post-1656379.

I want to query all vessels order by desc the number of sea services that each vessel has been selected as a post reference field.
I think I am missing something here. The correct association in relationship table is the reference with id 45.
"45 sea-service-vessel Sea Service Vessel Sea Service Vessel toolset posts 78 posts 77 post_reference_field"

#1657767

The post reference field is also based on one-to-many relationship, you can follow the document I mentioned above to setup PHP codes using function toolset_get_related_posts().

See the example codes I provided in your previous thread:
https://toolset.com/forums/topic/filtering-view-with-two-complex-relationships-get_related_posts-error/

#1659181

This is the code I finally used based on your help in order to keep a child counter on the parent posts.

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' ) {
       $relationship_slug = 'relationship_slug';
       $parent_id = toolset_get_related_post( $post_id, $relationship_slug, 'parent' );
       if($parent_id != 0)
       {
           $child_posts = toolset_get_related_posts(
                // post ID to which the results should be related.
                $parent_id, 
                // Relationship slug.
                $relationship_slug,
                // Additional arguments.
                array (
                    // Specify the role to query by and to return. 
                    // Notice that we can use "other" in this situation.
                    'query_by_role' => 'parent',
                    'return' => 'post_id',              
                    'role_to_return' => 'child'
                )
            );
    	   $totalChildCount = count($child_posts);   
           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' ) { 
      $relationship_slug = 'relationship_slug';
      $parent_id = toolset_get_related_post( $post_id, $relationship_slug, 'parent' );
      if($parent_id != 0)
        {
           $child_posts = toolset_get_related_posts(
                // post ID to which the results should be related.
                $parent_id, 
                // Relationship slug.
                $relationship_slug,
                // Additional arguments.
                array (
                    // Specify the role to query by and to return. 
                    // Notice that we can use "other" in this situation.
                    'query_by_role' => 'parent',
                    'return' => 'post_id',              
                    'role_to_return' => 'child'
                )
            );
    	   $totalChildCount = count($child_posts);    	
    	   $oldChildCount = get_post_meta( $parent_id, 'wpcf-child-count', true );
    	   if( $totalChildCount != $oldChildCount ){
    	       update_post_meta( $parent_id, 'wpcf-child-count', $totalChildCount );
    	   }
        }
    }
    if ( get_post_type( $post_id ) == 'parent-post-type-slug' ) {  
        $count=get_post_meta( $post_id, 'wpcf-child-count', true );
               if($count == ''){
                   add_post_meta($post_id, 'wpcf-child-count',0);
               }
    }
} 
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.