Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. function action_pre_get_comments($query) {
  2.     $product_category = wp_get_post_terms(get_the_ID(), 'product_cat');
  3.  
  4.     if(empty($product_category) && !is_array($product_category)) {
  5.         return $query;
  6.     }
  7.  
  8.     if (!is_admin() && is_singular(array('product'))) {
  9.         $args = array(
  10.                 'post_type' => 'product',
  11.                 'post_status' => 'publish',
  12.                 'ignore_sticky_posts' => 1,
  13.                 'posts_per_page' => -1,
  14.                 'tax_query' => array(
  15.                         array(
  16.                                 'taxonomy' => 'product_cat',
  17.                                 'field' => 'term_id',
  18.                                 'terms' => $product_category[0]->term_id,
  19.                                 'operator' => 'IN'
  20.                         ),
  21.                 ),
  22.                 'fields' => 'ids'
  23.         );
  24.         $products = new WP_Query($args);
  25.  
  26.         $query->query_vars['post_id'] = null;
  27.         $query->query_vars['post__in'] = $products->posts;
  28.     }
  29.     return $query;
  30. };
  31. add_action( 'pre_get_comments', 'action_pre_get_comments', 10, 1 );
  32.  
  33.  
  34.  
  35.  
  36. function add_sku_suffix_to_product_permalinks($slug, $post_ID, $post_status, $post_type, $post_parent, $old_slug) {
  37.     $post_title = get_the_title($post_ID);
  38.     $post_title = sanitize_title($post_title);
  39.  
  40.     if($post_status === 'publish' && $post_type == 'product') {
  41.         $product_sku = get_post_meta($post_ID, '_sku', true);
  42.         if($product_sku) {
  43.             $slug = $post_title . '-' . $product_sku;
  44.         }
  45.         else {
  46.             $slug = $post_title;   
  47.         }
  48.     }
  49.     return $slug;
  50. }
  51. add_filter('wp_unique_post_slug', 'add_sku_suffix_to_product_permalinks', 10, 6);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement