Skip Navigation

[Resolved] Add post relationship column to custom post list in admin menu /Child post count

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

Problem: I would like to add a column to the post list in wp-admin menu for a custom post type. In that column I would like to display the number of child posts in a specific post relationship.

Solution: Custom code is required to do this.

/******************************
 ** Adds post relationship column to salons table
 ******************************/
add_filter( 'manage_edit-salons_columns', 'my_edit_salons_columns' ) ;
function my_edit_salons_columns($defaults) {
    $defaults['child'] = 'Child';
    return $defaults;
}
   
/******************************
 ** Adds data to the post relationship column in salons table
 ******************************/
add_action( 'manage_salons_posts_custom_column', 'my_manage_salons_columns', 10, 2 );
function my_manage_salons_columns($column_name, $post_id) {
    if ($column_name == 'child') {
            $children = toolset_get_related_posts(
              $post_id,
              'salons-shops',
              'parent',
              1000000,
              0,
              array(),
              'post_id',
              'child'
            );
    echo sizeof($children);
    } 
}

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

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by koheiY 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#922843
parentpostlist.JPG
parentpage.JPG

Add post relationship column to custom post list in admin menu / Add the number of child posts

Thanks to this topic (https://toolset.com/forums/topic/add-post-relationship-column-to-custom-post-list-in-admin-menu/) , I can add a Parent column in a child custom post list in the admin menu.

On the other way, how can I add a Child column in a parent post list in the admin menu?

And I want to display the number of child posts in that column.

For example,
I have 2 post types; Salons and Shops.
I set one to many relationships.
Salons is one, Shops is many.
SalonA has a few shops: shop1, shop2 and shop3.

In the SalonA's admin page, I can see the 3 items in Post-relationship widget.
(Please find the attached file. parentpage.JPG)

I want to display this number in the Salon post list admin page.
(Please find the attached file. parentpostlist.JPG)

I added these code in my function.php.

/******************************
 ** Adds post relationship column to salons table
 ******************************/
add_filter( 'manage_edit-salons_columns', 'my_edit_salons_columns' ) ;
function my_edit_salons_columns($defaults) {
    $defaults['child'] = 'Child';
    return $defaults;
}
  
/******************************
 ** Adds data to the post relationship column in salons table
 ******************************/
add_action( 'manage_salons_posts_custom_column', 'my_manage_salons_columns', 10, 2 );
function my_manage_salons_columns($column_name, $post_id) {
    if ($column_name == 'child') {
            echo do_shortcode("[wpv-found-count item='@salons-shops.parent']");
            } 
    }


But I think this part is wrong.

echo do_shortcode("[wpv-found-count item='@salons-shops.parent']");

Would you give me some advice?

#923191

You have access to the parent post's ID in this callback, and you know the post relationship slug. You need to get all the child posts using that information, and display the count. You will be able to use the Post Relationships API toolset_get_related_posts to do this. We have documentation here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

For a relationship created in the new relationships system, an example might look like this:

$children = toolset_get_related_posts(
  $post_id,
  'salons-shops',
  'parent',
  1000000,
  0,
  array(),
  'post_id',
  'child'
);

echo sizeof($children);
#923249

Thank you Cox!

I changed the code and it works fine!

Ref:

/******************************
 ** Adds post relationship column to salons table
 ******************************/
add_filter( 'manage_edit-salons_columns', 'my_edit_salons_columns' ) ;
function my_edit_salons_columns($defaults) {
    $defaults['child'] = 'Child';
    return $defaults;
}
  
/******************************
 ** Adds data to the post relationship column in salons table
 ******************************/
add_action( 'manage_salons_posts_custom_column', 'my_manage_salons_columns', 10, 2 );
function my_manage_salons_columns($column_name, $post_id) {
    if ($column_name == 'child') {
			$children = toolset_get_related_posts(
			  $post_id,
			  'salons-shops',
			  'parent',
			  1000000,
			  0,
			  array(),
			  'post_id',
			  'child'
			);
	echo sizeof($children);
	} 
}