Skip Navigation

[Closed] Wrong title showing when no relationship set

This support ticket is created 3 years 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 7 replies, has 2 voices.

Last updated by Jamal 3 years ago.

Assisted by: Jamal.

Author
Posts
#2235379

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?

#2235435

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.

#2235513

Almost there. Now it's showing the Product ID for all products. Not the Artist or blank.

Screenshot: hidden link

#2237343

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;
}
?>
#2240761

I'm getting this error now:
hidden link

#2240849

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;
}
?>
#2240855

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?

#2241371

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?

The topic ‘[Closed] Wrong title showing when no relationship set’ is closed to new replies.