Tell us what you are trying to do?
I've got two custom post types called "object" and "apartement".
There ist a relationship 1 to many: 1 object can have many apartements.
Everything works fine, but ...
... I want to see the name of the related object in the list of the appartements.
At the Moment the list in the backend looks like:
Title date
------------------------------------------------------
Apart No 12 07.01.2024
Apart No 02 07.01.2022
This is what I want to achieve:
Title Object date
-----------------------------------------------------------------------------------
Apart No 12 Fine House Mainroad 07.01.2024
Apart No 02 Tiny House Backerstreet 07.01.2022
Is there any documentation that you are following?
no
Is there a similar example that we can see?
What is the link to your site?
hidden link
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
Well - there is no such feature available but you can manage this using the native WrodPress hooks:
To get the related parent post ID you can use the function: toolset_get_related_post()
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
Please try to add the following code to your current theme's functions.php file:
add_filter('manage_apartement_posts_columns', 'func_apartement_related_posts_columns');
function func_apartement_related_posts_columns($column){
$columns = array(
'title' => __( 'Title' ),
'object' => __( 'Object' ),
'date' => __( 'Date'),
);
return $columns;
}
add_action('manage_apartement_post_custom_column', 'func_show_related_posts_object_column',6,2);
function func_show_related_posts_object_column($column, $post_id ){
if ($column === 'object') {
$relationship_slug = 'your-post-relationship-slug';
$parent_post_id = toolset_get_related_post($post_id,$relationship_slug);
echo get_the_title($parent_post_id);
}
}
Where:
- replace $relationship_slug with original value of your post relationship slug
More info:
- hidden link
- https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column
- https://wordpress.stackexchange.com/questions/253640/adding-custom-columns-to-custom-post-types
Hello,
thank you very much for the support.
I had to change some things, but now the script works fine.
I created a little plugin for this.
Best regards,
Matthias
$value) {
if ($key == 'title') {
$new_columns[$key] = $value;
$new_columns['related_object'] = 'Objekt';
$new_columns['related_strasse'] = 'Straße';
} else {
$new_columns[$key] = $value;
}
}
unset($columns);
return $new_columns;
}
add_filter('manage_wohnung_posts_columns', 'add_object_column_to_apartments');
// Funktion zum Befüllen der benutzerdefinierten Spalten
function populate_object_column($column, $post_id) {
if ($column === 'related_object') {
$relationship_slug = 'objekt-wohnung';
$parent_post_id = toolset_get_related_post($post_id, $relationship_slug);
echo get_the_title($parent_post_id);
}
if ($column === 'related_strasse') {
$strasse = get_post_meta($post_id, 'wpcf-strasse', true);
if ($strasse) {
echo esc_html($strasse);
} else {
echo 'Keine Straße';
}
}
}
add_action('manage_wohnung_posts_custom_column', 'populate_object_column', 10, 2);
// Funktion zum Sortieren der benutzerdefinierten Spalten
function apartment_custom_columns_sortable($columns) {
$columns['related_object'] = 'related_object';
$columns['related_strasse'] = 'related_strasse';
return $columns;
}
add_filter('manage_edit-wohnung_sortable_columns', 'apartment_custom_columns_sortable');
?>