Skip Navigation

[Resolved] Display Admin Column from Related Parent Post Title

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

Problem:

The issue here is that the user wanted to display their parent post information in the Toolset Admin Column for the custom post type.

Solution:

This can be done by using the code below.


//Replace the following
/*
cptslug - replace with custom post type slug

parent - replace with parent cpt slug

child - replace with child cpt slug

Column Name - replace with the desired column name.
*/
//Add custom column
add_filter('manage_edit-cptslug_columns', 'my_columns_head');
function my_columns_head($defaults) {
$defaults['Parent'] = 'Column Name';//edit
return $defaults;
}
 
//Add rows data
add_action( 'manage_cptslug_posts_custom_column' , 'my_custom_column', 10, 2 );
function my_custom_column($column, $post_id ){
switch ( $column ) {
case 'Parent':
$parent_id = toolset_get_related_post( $post_id, array( 'parent', 'child' ) );//edit
if($parent_id){
echo get_the_title( $parent_id );
} else {
echo 'No Parent Found';
}
break;
}
}

This code can be added to your Toolset custom code settings at Toolset -> Settings -> Custom code and then activate it once you've added the code.

This support ticket is created 2 years, 5 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by simchaH 2 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#2225019
Screenshot 2021-11-19 122501.jpg
Screenshot (15).png
Screenshot (14).png

Hi, I found this code for displaying admin column for parent post title from the following thread:
https://toolset.com/forums/topic/how-to-get-wordpress-admin-columns-for-parent-post-title/

I copied it over and changed around the code for my site's CPT's, the column was created and is displaying, but for some reason I keep getting the "else" message of "No Maggid Shiur", even when I know there is a related parent post title. (see images below)

Do you see any issue with the code?

//Add custom column
add_filter('manage_edit-shiur_columns', 'my_columns_head');
function my_columns_head($defaults) {
$defaults['Parent'] = 'Maggid Shiur';
return $defaults;
}

//Add rows data
add_action( 'manage_shiur_posts_custom_column' , 'my_custom_column', 10, 2 );
function my_custom_column($column, $post_id ){
switch ( $column ) {
case 'Parent':
$parent_id = get_post_meta( $post_id, "_wpcf_belongs_person_id", true);
if($parent_id){
echo get_the_title( $parent_id );
} else {
echo 'No Maggid Shiur';
}
break;
}
}

#2225075

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Thank you for getting in touch.

The problem here is that this method is using the old method of getting the relationship and will no longer work after installing types 3.0

To get this working again it will need to be modified with the function below.
toolset_get_related_post()

Try using this code.

//Add custom column
add_filter('manage_edit-shiur_columns', 'my_columns_head');
function my_columns_head($defaults) {
$defaults['Parent'] = 'Maggid Shiur';
return $defaults;
}

//Add rows data
add_action( 'manage_shiur_posts_custom_column' , 'my_custom_column', 10, 2 );
function my_custom_column($column, $post_id ){
switch ( $column ) {
case 'Parent':
$parent_id = toolset_get_related_post( $post_id, array( 'person', 'shiurim' ) );
if($parent_id){
echo get_the_title( $parent_id );
} else {
echo 'No Maggid Shiur';
}
break;
}
}

You may need to replace "shiurim" with the actual correct slug for the post type.

Please let me know if this helps.
Thanks,
Shane

#2225105

Hi Shane,

Thanks for getting back to me so soon. I swapped out the code and changed the 'shiurim' to 'shiur' on this line (shiur is the slug name): $parent_id = toolset_get_related_post( $post_id, array( 'person', 'shiur' ) );

But when I save the functions.php file it is still showing up as "No Maggid Shiur".
Any ideas?

#2225133

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

This is going to require some further debugging.

Would you mind allowing me to have admin access to the site to check on this in further details for you ?

I've enabled the private fields for your next response.

Thanks,
Shane

#2226955

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Sim,

Checking this it appears the "No Maggid Shiur" is appearing under posts where there isn't any Parent.

From what I can see the code is working as intended as the name of the parent shows up for posts that have parents.

Thanks,
Shane

#2227013

Not sure why when I tested it earlier it wasn't displaying.. perhaps it was a simple caching issue. Either way, I double checked and I see it working now, so thanks.

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.