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);
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
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);
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/
Yes it was simply using get_the_ID() for the looping post id. My issue is resolved now. Thank you!