Skip Navigation

[Resolved] Difference between wp_toolset_associations and wp_toolset_associations_old

This support ticket is created 2 years, 12 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 3 voices.

Last updated by Christian Cox 2 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#2032683

Tell us what you are trying to do?

I have recently run the migration of the relationships setup. From what I can see all has appeared to have updated successfully. However, I need to do a manual SQL query using the wp_toolset_associations table. When looking at the 2 tables, they both appear to have exactly the same number of records however, both are completely different and the table "wp_toolset_associations_old" whereas the new table is incorrect. Has something gone wrong? Should I not worry and just query the table "wp_toolset_associations_old"?

Wordpress backend appears correct.

Is there any documentation that you are following?
https://toolset.com/forums/topic/table-wp_toolset_associations_old/ - from this post it appears something has gone wrong?

Is there a similar example that we can see?

What is the link to your site?
hidden link

#2032763

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

The _old version of the database is simply retained as a backup in case something goes wrong with the migration. If after using the site for a while the relationship data appears intact, it is safe to delete it.

Naturally, we don't recommend interacting with these tables directly, there is an API available: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

You will see there is a new table wp_toolset_connected_elements. The IDs in the association table now point to this new table rather than directly to wp_posts, the new table is a bridge between the two. (The changes were introduced for increased flexibility with relationships on multilingual sites.)

The _old table is "dead", it doesn't get updated with new data.

#2033901

Ok, thanks for the update on this. I need to make SQL queries as some of the interaction takes place outside of the main website.

I am also getting a number of fatal errors when using "toolset_get_related_post" - I am just substituting where I was using get_post_meta( $ID, '_wpcf_belongs_...is this a known issue?

#2034123

Hello, I'm not aware of a known issue here, I would need to see the full context and code to give you some advice for implementing this API. Can you share the full code here for me to review?

#2043879

I really need to apply an sql query. Is there a simple SQL query to output the child results in a one to many relationship from the id of the one side?

#2044793

Custom SQL queries are a bit outside the scope of support we offer here in the forums when there are built-in features available for the same purpose. I don't have a simple cut-and-paste SQL script available, but I can show you another way to get related post IDs using our post relationships API toolset_get_related_posts.

Add the following code to your child theme's functions.php file, or append the code to the end of a new snippet in Toolset Settings > Custom Code tab, set the snippet to run everywhere, and activate the snippet.

/** 
 * Output a comma-separated list of child post IDs given parent post ID and slug of O2M
 * https://toolset.com/forums/topic/difference-between-wp_toolset_associations-and-wp_toolset_associations_old/
 * @relslug, string, required: Slug of the o2m post relationship
 * @paramid, integer, required: ID of parent post
 * 
 * EXAMPLE
 * Child post IDs: [tssupp-get-child-ids relslug="your-relationship-slug" parentid="12345"][/tssupp-get-child-ids]
 */
add_shortcode( 'tssupp-get-child-ids', 'tssupp_get_child_ids_func');
function tssupp_get_child_ids_func($atts)
{
  $limit = 1000;
  $default_rel_slug = 'your-relationship-slug';

  // you should not edit below this line

  $a = shortcode_atts( array(
    'relslug' => $default_rel_slug,
    'parentid' => 0,
  ), $atts );
  $children = toolset_get_related_posts(
    $a['parentid'],
    $a['relslug'],
    [
      'query_by_role'   => 'parent',
      'limit'           => $limit,
      'offset'          => 0,
      'args'            => [],
      'role_to_return'  => 'other',
    ],
  );
  $child_list = implode(',', $children);
  return $child_list;
}

The limit is currently set at 1000 related posts, but you can increase that for larger sites if necessary. A positive integer value is required instead of 1000.

Use the shortcode like so to output a comma-separated list of child post IDs for a known parent post ID in a specific post relationship:

Comma-separated list of child post IDs for parent post ID <strong>12345</strong> in post relationship <strong>your-relationship-slug:</strong><br />
[tssupp-get-child-ids relslug="your-relationship-slug" parentid="12345"][/tssupp-get-child-ids]<br />

Comma-separated list of child post IDs for parent post ID <strong>67890</strong> in post relationship <strong>your-other-relationship-slug:</strong><br />
[tssupp-get-child-ids relslug="your-other-relationship-slug" parentid="67890"][/tssupp-get-child-ids]<br />

You can see in the example here how to use the shortcode multiple times to output the child post IDs for mulitple parent posts from different post relationships. It's a simple but effective way to get the post IDs you described..

Documentation for the post relationships API toolset_get_related_posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

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