Skip Navigation

[Resolved] Post relationship not working

This support ticket is created 4 years, 6 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 – 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: Asia/Karachi (GMT+05:00)

This topic contains 8 replies, has 2 voices.

Last updated by ivanT-7 4 years, 6 months ago.

Assisted by: Waqar.

Author
Posts
#1648151
relationship not working.jpg

I have created 5 post type, and related them one-to-many

Lately I noticed that one of these relationship is failing to connect some (and only some) posts. Tried setting it to draft and back to published, or updating the posts to no avail.

If I create a new post the relationship works well. It just happens to some of them... Any idea why?

#1648435

Hi,

Thank you for contacting us and I'd be happy to assist.

To troubleshoot this, I'll need to see how these posts and the post relationships are set up in the admin area.

Can you please share temporary admin login details along with a link to a few posts where this issue can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1649111

Hi Ivan,

Thank you for sharing the admin access.

I've performed some tests on my website and can confirm that in case of 1-to-many relationship, if a parent post in the relationship is trashed (but not permanently deleted), the relationship meta box on the child post's edit screen, no longer shows the option to create a new connection with some other parent post.
( as shown in your screenshot )

However, when that parent post is permanently deleted from the admin area, its post connections are automatically deleted too and after that, the child posts are free to connect to different parent posts.

On your website though, I don't see any "Routes" posts in the trash which could happen if they have been deleted directly from the database or if data was imported/exported from a different source.

Do you recall deleting any posts directly from the database or running some import/export for the posts?

If you could share a clone/snapshot of your website, I can help in tracking all the "Image Route" connections with the missing parent post.
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )

Note: I've set your next reply as private again.

regards,
Waqar

#1654341

Hi Ivan,

Thank you for sharing these details.

I've downloaded the duplicator package and will be reviewing the databased to narrow down to post connections involving deleted route posts.

I will update you as soon as this review and research is complete.

Thank you for your patience.

regards,
Waqar

#1662983

Hi Ivan,

Thank you for waiting.

During my testing and research, I managed to remove the relationship connections between the deleted or non-existingng images and route posts, through a custom shortcode.

Here are the steps:

1. Please create a full backup copy of the website.

2. At the bottom of your child theme's "functions.php" file, include this custom shortcode's snippet:


add_shortcode('temp-shortcode', 'temp_shortcode_func');
function temp_shortcode_func() {

global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM `cs_toolset_associations` WHERE relationship_id='5'", ARRAY_A );

	ob_start();

	$correct = 0;
	$wrong = 0;
	foreach ($results as $key => $value) {
		if(empty(get_the_title($value['parent_id']))) {
			$wrong++;
			$wpdb->delete( 'cs_toolset_associations', array( 'id' => $value['id'] ) );
		}
		else
		{
			$correct++;
		}
		$i++;
	}

echo "Total results:".count($results)."<br>";
echo "Wrong results:".$wrong."<br>";
echo "Correct results:".$correct."<br>";

return ob_get_clean();

}

3. Create a new temporary page and in its content, add this shortcode:


[temp-shortcode]

4. After saving the page, please visit the page's front-end and you'll see the message about the total relationship results found for "Image Route" and how many of theme were correct or incorrect. The code will also automatically remove the incorrect results from the database.

5. Once these steps are completed, you can delete this temporary page and remove the code snippet from the "functions.php" file.

As a result, when you'll visit the route posts which were not showing you the option to attach any image post earlier, they will be fixed and you'll be able to attach the image post now.

I hope this helps and please let me know how it goes. For more personalized assistance around custom code and database operations, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1664695

Hi Waqar,
thanks for the code, but it seems it doesn't work.
I added the snippet to functions.php and created a new page with the code, but when visualized it says
Total results: 0
Wrong results: 0
Correct resuts:0

The relationship is still broken on many posts. If you want to try yourself I have backed up the site
Ivan

#1666711

Hi Ivan,

I've checked the custom code again on your website's clone and it works as expected.

If you can access the database management tool like phpMyAdmin for your actual website, can you please confirm the following points:

1. The database's table prefix is "cs_".
( screenshot: hidden link )

2. In the database, look for a table named "{table_prefix}_toolset_relationships" and see if the "id" for the "image-route" relationship is '5'
( screenshot: hidden link )

If you see a different table prefix or relationship id, please update it in the code snippet accordingly.

regards,
Waqar

#1666761

Hi Waqar,

the only difference was that the prefix of my database has capital letters ( CS_toolset_relationship) hidden link

I corrected the code you provided and it worked! hidden link

Thank you so much!
Ivan

#1666769

My issue is resolved now. Thank you!