Skip Navigation

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

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

Problem:
Add post relationship column to custom post list in admin menu

Solution:
You can use standard wordpress hook "manage_{post-type-slug}_posts_custom_column" to add the parent post title as admin column.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/add-post-relationship-column-to-custom-post-list-in-admin-menu/#post-921565

Relevant Documentation:
=> https://toolset.com/forums/topic/display-post-type-relationship-as-column-in-admin-table/

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.

Our next available supporter will start replying to tickets in about 0.28 hours from now. Thank you for your understanding.

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 6 replies, has 2 voices.

Last updated by koheiY 6 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#921272

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.

I want to add post relationship column to custom post list in admin menu.
The page of url is (/wp-admin/edit.php?post_type=shops).

I added codes below:

/******************************
 ** Adds post relationship column to shops table
 ******************************/
add_filter( 'manage_edit-shops_columns', 'my_edit_shops_columns' ) ;
function my_edit_shops_columns($defaults) {
    $defaults['parent'] = 'Parent';
    return $defaults;
}

/******************************
 ** Adds data to the post relationship column in shops table
 ******************************/
add_action( 'manage_shops_posts_custom_column', 'my_manage_shops_columns', 10, 2 );
function my_manage_shops_columns($column_name, $post_id) {
    if ($column_name == 'parent') {
			$parent_id = get_post_meta( $post_id, "@salons-shops.parent", true);
			if($parent_id){
			echo get_the_title( $parent_id );
			} else {
			 echo 'No parent';  
			}
    }
}

But I got the result "No parent" in the row of shop1, shop2 and shop3.
I want to get the result that shows their parent post title i.e. "SalonA".

I created this code from these threads;
https://toolset.com/forums/topic/how-to-get-wordpress-admin-columns-for-parent-post-title/
https://toolset.com/forums/topic/display-post-type-relationship-as-column-in-admin-table/

But these description are before types3.0.

#921565

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - could you please try to use following code. I've modified the code and added the following line of code to display the parent title:

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

Please try to use following code and try to resolve your issue.

 ** Adds post relationship column to shops table
 ******************************/
add_filter( 'manage_edit-shops_columns', 'my_edit_shops_columns' ) ;
function my_edit_shops_columns($defaults) {
    $defaults['parent'] = 'Parent';
    return $defaults;
}
 
/******************************
 ** Adds data to the post relationship column in shops table
 ******************************/
add_action( 'manage_shops_posts_custom_column', 'my_manage_shops_columns', 10, 2 );
function my_manage_shops_columns($column_name, $post_id) {
    if ($column_name == 'parent') {
            echo do_shortcode("[wpv-post-title item='@salons-shops.parent']");
            } else {
             echo 'No parent';  
            }
    }
}
#921583
parent.JPG

Thank you for your help.
It succeeded to display their parent post title (DKBC), but it affected other columns.
'No Parents' appeared in Yoast SEO columns...
Please find the attached.

#921594

Minesh
Supporter

Languages: English (English )

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

Well - this is a custom code, I do not know its affect but what if you try to remove else statement or add exit; just after the echo statement.

#921604

Works fine! Thank you!

#921607

Minesh
Supporter

Languages: English (English )

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

Glad to know that solution I shared works perfectly for you and help you to resolve your issue. Would you mind to resolve the ticket with happy face 🙂

#921666

Thank you!