Skip Navigation

[Resolved] Connect posts to parent by custom field

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

Author
Posts
#2156601

Tell us what you are trying to do?
I have many posts imported hourly to my site, the CPT slug is 'jci-hazard-product'. I need to connect them to this page: hidden link

We were doing it with this plugin: hidden link
But now running the shortcode to connect the posts is causing a fatal error with the following being logged to the error log:

[01-Sep-2021 01:15:11 UTC] PHP Fatal error: Uncaught Toolset_Element_Exception_Element_Doesnt_Exist: Unable to load posts 3931743 (3931743). in /home/pdccpanel/public_html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/element/post.php:83
Stack trace:
#0 /home/pdccpanel/public_html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/element/element_factory.php(104): Toolset_Post->__construct(3931743, NULL, NULL, NULL, Object(Toolset_Element_Factory))
#1 /home/pdccpanel/public_html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/element/element_factory.php(83): Toolset_Element_Factory->get_post_untranslated(3931743)
#2 /home/pdccpanel/public_html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/element/element_factory.php(53): Toolset_Element_Factory->get_post(3931743)
#3 /home/pdccpanel/public_html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/m2m/association/association.php(268): Toolset_Element_Factory->get_element('posts', 3931743)
#4 /hom in /home/pdccpanel/public_html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/element/post.php on line 83

Both the Parent post and child posts contain a custom field (slug 'haz-id') with the same value. I'd like to use this custom field and a variation of the code provided by Christian here: https://toolset.com/forums/topic/bulk-assign-child-posts-to-parent-post/#post-1247104 to create a connection of the posts so I don't have to rely on this third party plugin.

Could you help me with that?
Ideally, the code could be run with a shortcode so I can run it with a cron-job right after the imports run.

Thank you!

#2156771

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

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

To suggest the next steps, I'll need to see exactly how these custom fields and the post-relationship is set up.

Can you please share temporary admin login details, along with the link to an example parent and child posts where the same custom field value exists?

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

regards,
Waqar

#2162125

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting and for sharing these details.

During testing on my website, I was able to make this work, using the following custom shortcode:


add_shortcode('process-mh-relationships', 'process_mh_relationships_func');
function process_mh_relationships_func( $atts ) {

	$a = shortcode_atts( array(
		'id' => ''
	), $atts );

	if (!empty($a['id'])) {

		$parent_post_slug = 'major-hazard';
		$child_post_slug = 'jci-hazard-product';
		$relationship_slug = 'manual-major-hazard-jci-hazard-product';

		// get target major Hazard post's 'haz_id' custom field values
		$parent_haz_ids = types_render_field( "haz_id", array( 'item' => $a['id'], 'separator' => ',') );
		// create an array of those values
		$parent_haz_ids_arr = explode(',', $parent_haz_ids);

		// get existing related hazard products so that they can be excluded from new processing
		$get_related_hazard_results = toolset_get_related_posts( $a['id'], $relationship_slug, 'parent', 9999999, 0, array(), 'post_id', 'child' );

		// get the list of all hazard product posts, where the 'haz_id' custom field value is the same as the target major Hazard post, excluding those which are already connected
		$args = array(
			'posts_per_page' => -1,
			'post_type'      => $child_post_slug,
			'post_status'    => 'publish',
			'post__not_in'   => $get_related_hazard_results,
			'fields'	     => 'ids',
			'meta_query'	 => array(
									array(
									'key'     => 'wpcf-haz_id',
									'value'   => $parent_haz_ids_arr,
									'compare' => 'IN',
									),
								),
		);
		$child_post_list = get_posts( $args );

		// if some child post are found which need to be connected
		if (!empty($child_post_list)) {
			foreach ( $child_post_list as $post_item ) {
				// connect those posts
				$connect_results[] = toolset_connect_posts( $relationship_slug, $a['id'], $post_item );
			}

			ob_start();
			echo '<pre>';
			print_r($connect_results);
			echo '</pre>';
			return ob_get_clean();

		}
		else
		{
			return "No posts were found which need to be connected!";
		}

	}
	else
	{
		return "No parent post ID provided";
	}
	
}

To run it for a parent "COVID-19 Global Pandemic" major hazard post with ID "255519", you can use the shortcode as:


[process-mh-relationships id='255519']

The shortcode will first get all the values of the custom field "haz_id" from this parent post and then look for all the JCI hazard posts which have the same "haz_id" field values and are not already connected to this target parent post. In the last step, those matched posts are connected to the parent post in the relationship too.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

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