After a new comment is saved, I’m able to redirect via functions.php
add_filter('comment_post', 'myredirect');
I want to redirect to the product category listing, say: http://example.org/baktec27/?product_cat=1a
So I need the category for the current post. Couldn’t access $post from functions.php so got post_id from $GLOBALS
$post_id = $GLOBALS[_POST][comment_post_ID]; // ok
// according to https://developer.wordpress.org/reference/functions/get_the_category/
// also tried wp_get_post_categories( $post_id )
$categories = get_the_category($post_id);
if ( ! empty( $categories ) ) {
file_put_contents('testfile2.txt', esc_html( $categories[0]->name));
} else {
file_put_contents('testfile2.txt', "NOTHING HERE");
}
I get an empty $categories.
Can you help? Thanks a lot!
I got it solved via get_the_terms using this snippet from WooCommerce - get category for product page
Foundit when i looked for product instead of post. :-)