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.
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';
}
}
}
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.
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.
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 🙂