79284786

Date: 2024-12-16 13:10:57
Score: 0.5
Natty:
Report link

The issue has been resolved. This code snippet may be helpful to others who encounter similar problems.

function my_query_by_post_types1($query) {
   if (is_singular('product')) {
       // Get product categories
       $terms = get_the_terms(get_the_ID(), 'product_cat');
       
       if ($terms) {
           // Get all category names
           $category_names = wp_list_pluck($terms, 'name');
           
           if ($category_names) {
               $query->set('post_type', 'news');
               $query->set('meta_query', array(
                   array(
                       'key'     => 'product_category',
                       'value'   => '"' . implode('","', $category_names) . '"',
                       'compare' => 'LIKE'
                   )
               ));
           }
       }
   }
}
add_action('elementor/query/product_related_news', 'my_query_by_post_types1');

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: segaf