Skip Navigation

[Resolved] filter with referenced post taxonomy

This support ticket is created 2 years, 10 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 25 replies, has 3 voices.

Last updated by davidS-72 2 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#2318201

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

#2318513

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

#2318753

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

#2318765

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

#2319241

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

#2319549

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

#2319703

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

#2320957

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

#2322913
4 - CPT ACCIONS FORMATIVES - ADMIN - the DESTINATION.jpg
1 - the 2 post types.jpg
2 - the taxonomy to copy.jpg
3 - CPT cursos - the FROM to copy.jpg
3.1 - CPT cursos ADMIN - the FROM to copy.jpg
5 - custom code.jpg

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

#2324347

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

#2325101

My issue is resolved now.
Shane,
YOU'RE GREAT!

thanks,
David