I'm following up to this thread: https://toolset.com/forums/topic/export-custom-post-type-data-to-csv/
I did the same thing as suggested but found that when there's no relationship set, the code returns the title of the current post.
In my set up I have Artists (one) and Products (many). When the Product doesn't have a related Artist, the code in the thread above returns the title of the Product. I want it to return nothing.
Can you help?
Hello and thank you for contacting the Toolset support.
Try the following code instead:
<?php
function prov_parent_title($id)
{
$parent_id = toolset_get_related_post( $id, 'relationship-slug', 'parent');
$prov_title = $parent_id ? get_the_title($parent_id) : "";
return $prov_title;
}
?>
I hope this helps. Let me know if you have any questions.
Almost there. Now it's showing the Product ID for all products. Not the Artist or blank.
Screenshot: hidden link
Can you try the following for line 5:
$prov_title = ( $parent_id > 0 ) ? get_the_title($parent_id) : "";
If it does not work try this:
<?php
function prov_parent_title($id)
{
$parent_id = toolset_get_related_post( $id, 'relationship-slug', 'parent');
$prov_title = "";
if ( $parent_id > 0 ) {
$prov_title = get_the_title($parent_id)
}
return $prov_title;
}
?>
I'm getting this error now:
hidden link
You will need to add a semicolon (;) at the end of line 7. Try this:
<?php
function prov_parent_title($id)
{
$parent_id = toolset_get_related_post( $id, 'relationship-slug', 'parent');
$prov_title = "";
if ( $parent_id > 0 ) {
$prov_title = get_the_title($parent_id);
}
return $prov_title;
}
?>
The error is fixed, but the code still returns the ID of the child. Not the parent.
Does this have something to do with the fact that the child is a Woocommerce product and the parent is a CPT?
No, I don't think it has something to do with WooCommerce products. After all, they are just a custom post type with the slug "product".
Would you allow me temporary access to your website to check this further? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Please let me know:
- Where the code is added?
- Where the code is used? A shortcode? A custom hook?