Tell us what you are trying to do?
I want to redirect some (not all) of my terms pages of a custom taxonomy via wp_redirect. Here is part of my working code:
if ( is_tax( 'my-custom-taxonomy', 'a-term-of-my-taxonomy' ) ) {
wp_redirect( 'a-special-target-page-for-that-term', 301 ); // this line is where I need the contents of the custom term field
exit();
}
Is there any documentation that you are following?
No, couldn't find anything for that special case.
Is there a similar example that we can see?
-
What is the link to your site?
Term archive that I want to redirect to another page: hidden link
Custom field for terms: hidden link
Term in backend with custom field for redirect target filled in: hidden link
I hope you understand what I mean 🙂
You can use the same login credentials that I provided for the last ticket.
The final working code could look something like this (pseudo code!):
if ( is_tax( 'my-custom-taxonomy', 'a-term-of-my-taxonomy' ) ) {
wp_redirect( get_toolset_custom_field('my_custom_redirect'), 301 );
exit();
}
Hello and thank you for contacting the Toolset support.
You may want to use the types_render_termmeta function from Toolset Types API.
https://toolset.com/documentation/user-guides/views/displaying-wordpress-term-fields/#displaying-a-field-for-a-specific-taxonomy-term
Check this sample example:
add_filter('template_redirect', 'template_redirect_filter', 10);
function template_redirect_filter( $url ) {
if ( is_tax( 'book-author', 'tester' ) ) {
$term = get_term_by( 'slug', 'tester', 'book-author' );
$term_field = types_render_termmeta("author-contact", array( "term_id" => $term->ID ) );
$url = site_url( '/?hello=' . ( $term_field ? $term_field : 'nada' ) );
wp_safe_redirect( $url, 301 );
exit;
}
return $url;
}
I hope this helps. Let me know if you have any questions.
I didn't quite understand the example without context but could work out a solution from it. Here is my final working code:
add_filter('template_redirect', 'drk_abteilung_terms_template_redirect_filter', 10);
function drk_abteilung_terms_template_redirect_filter( $url ) {
if ( is_tax( 'abteilung' ) ) { // only for taxonomy "abteilung"
// now get our custom term field 'abteilung-custom-redirect', which is a URL field type
$term_field = types_render_termmeta("abteilung-custom-redirect", array( "term_id" => $term->ID, "output" => "raw" ) ); // use raw to avoid <a> tag - more info here https://toolset.com/documentation/customizing-sites-using-php/functions/#url
if ( $term_field ) { // if it is not empty
$url = site_url( '/' . $term_field ); // redirect to URL of our custom field 'abteilung-custom-redirect'
wp_safe_redirect( $url, 301 );
exit();
}
}
return $url;
}
My issue is resolved now. Thank you!