Skip Navigation

[Resolved] Cred form function to add custom taxonomies of current post to related post

This thread is resolved. Here is a description of the problem and solution.

Problem:

Assign all my current post taxonomy values to each newly selected post.

Solution:

It needs custom codes, here are my suggestions:

https://toolset.com/forums/topic/cred-form-function-to-add-custom-taxonomies-of-current-post-to-related-post/#post-1271415

Relevant Documentation:

https://codex.wordpress.org/Function_Reference/wp_set_object_terms

This support ticket is created 5 years, 5 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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by martinE-4 5 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1271339
Datastan_Meets_Storyland__Surfing_the_Zeitgeist_Without_Wiping_Out_–_ICASC_MIgration_1.jpg

I want to assign all my current post taxonomy values to each newly selected post.

In the uploaded image I have an example with 3 currently related posts to the current one. If I add a 4th post to the taxonomy of the current one, I would like not only my current post taxonomy to be added to the newly selected post, but the existing 3 related posts as well. (their taxonomy / post ids are already assigned to the current post).

function code at this point:

function set_term_from_generic_select( $post_id, $form_data ) {
$forms = array( 10191 );
  if ( in_array( $form_data['id'], $forms )) {
    $taxonomy = 'related';
    $tag = $_POST['related-resource'];
    wp_set_object_terms( $post_id, $tag, $taxonomy, true );
    $post_slug = get_post_field( 'post_name', $post_id );  
    wp_set_object_terms( $tag, strval($post_id), $taxonomy, true );
  }
}
add_action('cred_save_data', 'set_term_from_generic_select',100,2);
#1271415

Hello,

How do you setup the relationship between post types?
I assume we are talking about this case:
Two post types:
- Post type A
- Taxonomy "related", which is registered to above post type.

You are going to setup many-to-many relationship between posts of the same post type by taxonomy "related".

If it is, it needs custom codes, here are my suggestions:

1) get IDs of those 3 existed posts by by taxonomy "related"
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

2) Make a loop, setup their terms of taxonomy "related" as what you want.
https://codex.wordpress.org/Function_Reference/wp_set_object_terms
$append
(bool) (optional) If true, terms will be appended to the object. If false, terms will replace existing terms

#1272271

My function purpose again is (using a custom taxonomy 'related' with terms = post id's of related posts):
1. relate the current post with the generic dropdown selected resource post ($tag) - DONE (1st wp_set_object_terms)
2. relate the generic dropdown selected resource post with the current post ($post_id) - DONE (2nd wp_set_object_terms)
3. loop through the previously related posts (that already are related to our current post) and append this newly selected related post id to their taxonomy terms as well.

I am successful with 1 and 2. But 3 does not work yet.

Specifically, my new query of related posts that have the taxonomy term of the current post $post_id, and then looping through that query to append to each loop post taxonomy with the generic selected post id from the CRED dropdown select field ('related-resource'), does not work yet.

Can you spot my error?

function set_term_from_generic_select( $post_id, $form_data ) {
$forms = array( 10191 );
  if ( in_array( $form_data['id'], $forms )) {
    $taxonomy = 'related';
    $tag = $_POST['related-resource']; //get the term of the resource to 'relate' from the generic select term in the form generic dropdown select
	 wp_set_object_terms( $post_id, $tag, $taxonomy, true ); //set tax term of current post
	  wp_set_object_terms( $tag, strval($post_id), $taxonomy, true ); //set tax term of related post with current post id
	  //now we have set up a bi-directional 'relationship' using the taxonomy 'related' term with post_id as key
    //now we have set up a bi-directional 'relationship' using the taxonomy 'related' term with post_id as key
    //next we need to query posts that already have the current post id in their 'related' taxonomy and append the selected post to their 'related' taxonomy
$args = array(
	'post_type' => 'resource',
	'tax_query' => array(
		array(
			'taxonomy' => 'related',
			'field'    => 'slug',
			'terms'    => strval($post_id),
		),
	),
); 
$query1 = new WP_Query( $args );
// we loop
if ( $query1->have_posts() ) {
	// The Loop
	while ( $query1->have_posts() ) {
		$query1->the_post();
		wp_set_object_terms( $post_id, $tag, $taxonomy, true ); //set tax term of loop post with generic field post id, $tag from earlier
	}
	
	/* Restore original Post Data 
	 * NB: Because we are using new WP_Query we aren't stomping on the 
	 * original $wp_query and it does not need to be reset with 
	 * wp_reset_query(). We just need to set the post data back up with
	 * wp_reset_postdata().
	 */
	wp_reset_postdata();
}
   
}

}
add_action('cred_save_data', 'set_term_from_generic_select',100,2);

#1272393

I have checked the PHP codes you mentioned above, line 27, you should use current post ID get_the_ID() instead of the new post ID $post_id, for example:

wp_set_object_terms( get_the_ID(), $tag, $taxonomy, true ); 

Since it is a custom codes problem, if you need assistance for it, please provide a test site with the same problem, also point out the problem page URL and form URL, where I can edit your PHP codes, I need to test and debug it in a live website.

You can also check it with our Toolset contractors:
https://toolset.com/contractors/

#1272751

Yes it was simply using get_the_ID() for the looping post id. My issue is resolved now. Thank you!