Ok, I undertand. Then, I need to do this filter and I see an alternative solution but I don't know how to do it.
The solution is: when user save a post "Acció formativa", copying (by hook?) the taxonomy "Temàtiques dels cursos" from the referenced "Curs" to a new field in "Acció formativa".
Then I'll be able to filter with this new field in "Acció formativa".
What do you think?
thanks,
David
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi David,
If I understand correctly, when the user saves the post you want the taxonomy of the post that was saved in the post reference field to be copied to the post that is being created correct?
If that is the case then this can work given that the taxonomy is also assigned to the post being created. If you're familiar with php then you can use the hook below to do this when the post is being created on the backend.
https://developer.wordpress.org/reference/hooks/save_post/
Please let me know if my interpretation is correct.
Thanks,
Shane
I've got dificulties to get and put the taxonomy (is Hierarchical). Please, can you help me ?
My code is:
function func_save_custom_field_value( $post_id, $post, $update ) {
$parent = toolset_get_related_post($post_id,'curs-de-referencia');//that works fine
//Here are the problem
//get the taxonomy with slug=tematica-de-curs (is Hierarchical) list of terms from the parent
$post_categories = get_the_terms( $parent, 'tematica-de-curs' );
//put the taxonomy list of terms in the post
wp_set_post_terms($post_id, $post_categories);
}
add_action('save_post', 'func_save_custom_field_value', 99, 3 );
thanks,
David
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi David,
Based on what you're saying, you're having issues with the code at this line
$post_categories = get_the_terms( $parent, 'tematica-de-curs' );
Can you perform a var_dump() function on the $post_categories variable so that I can see what is being stored in this variable?
Thanks,
Shane
Hi Shane,
the problem is this two spteps,This code doesn't works:
1) get the parent categories (slug category is 'tematica-de-curs') :
$post_categories = get_the_terms( $parent, 'tematica-de-curs' );
2) put this categories to the post user is editing:
wp_set_post_terms($post_id, $post_categories);
The two CPT uses the same taxonomy 'tematica-de-curs'.
Basically, I don't know how to get and put the categories.
** HOW can I do that You said?: Can you perform a var_dump() function on the $post_categories variable so that I can see what is being stored in this variable?
thanks,
David
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi David,
Can you perform a var_dump so that I can see what is being returned by the get_the_terms() function.
Add this to your code.
var_dump($post_categories);
Also perform a var_dump on the $parent variable as well so we can ensure that we are getting the parent ID.
Thanks,
Shane
I've add this code:
var_dump($post_categories);
var_dump($parent);
var_dump($post_id);
but I can't see nothing on screen when I press "publish" button in the post.
Where/How can I see the var_dump output?
thanks
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi David,
I ran through your code again and found that you're using the relationship slug "curs-de-referencia" in the toolset_get_related_post() function.
However checking on your website i found that there are no relationships with that relationship slug. We may need to slow down a bit and Identify the 2 post types that we are working with and which post type you want to copy the taxonomy from.
Can you provide this information for me. Also the only relationship I see in your settings is the relationship between Docents and Accions formatives
Thanks,
Shane
Hi Shane,
OK, I attached screenshots with the information.
When the user "save" a post type "Accions formatives" (witch has a referenced post type "cursos") I want to copy the taxonomy from cursos to accions formatives.
thanks for your time,
David
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi David,
I found the issue .
The problem was that you didn't add the taxonomy information to the wp_set_post_terms()
wp_set_post_terms($post_id, $post_category->term_id,'tematica-de-curs');
Thanks,
Shane
My issue is resolved now.
Shane,
YOU'RE GREAT!
thanks,
David