linccce

Widget which returns always post_type...

Jan 15th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.69 KB | None | 0 0
  1. With widget below I get this query:
  2. SELECT   wp_posts.* FROM wp_posts
  3.   INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
  4. WHERE 1=1  AND wp_posts.post_name = 'sidebar'
  5.       AND (
  6.   ( wp_postmeta.meta_key = '_visibility'
  7.     AND CAST(wp_postmeta.meta_value AS CHAR)
  8.         IN ('catalog','visible')
  9.   )
  10. )
  11.       AND wp_posts.post_type = 'product'
  12.       AND ((wp_posts.post_status = 'publish'))
  13. GROUP BY wp_posts.ID
  14. ORDER BY wp_posts.post_date DESC
  15.  
  16. I want this query:
  17. SELECT   wp_posts.* FROM wp_posts
  18.   INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
  19. WHERE 1=1 AND ( ( wp_postmeta.meta_key = '_visibility'
  20.     AND CAST(wp_postmeta.meta_value AS CHAR)
  21.         IN ('catalog','visible')
  22.   ) ) wp_posts.post_type = 'product'
  23.       AND ((wp_posts.post_status = 'publish'))
  24. GROUP BY wp_posts.ID
  25. ORDER BY wp_posts.post_date DESC
  26.  
  27. class Products_By_Current_Cat_Widget extends WC_Widget {
  28.  
  29.     /**
  30.      * Constructor
  31.      */
  32.     public function __construct() {
  33.         $this->widget_cssclass    = 'woocommerce products_by_currenct_cat';
  34.         $this->widget_description = __( 'Display a list of your products on current product page from its parent directory.', 'lauksva' );
  35.         $this->widget_id          = 'products_by_currenct_cat';
  36.         $this->widget_name        = __( 'Current Product Parent Category Products', 'lauksva' );
  37.         $this->settings           = array(
  38.             'title'  => array(
  39.                 'type'  => 'text',
  40.                 'std'   => __( 'Products', 'lauksva' ),
  41.                 'label' => __( 'Title', 'lauksva' )
  42.             ),
  43.             'number' => array(
  44.                 'type'  => 'number',
  45.                 'step'  => 1,
  46.                 'min'   => 1,
  47.                 'max'   => '',
  48.                 'std'   => 5,
  49.                 'label' => __( 'Number of products to show', 'lauksva' )
  50.             ),
  51.         );
  52.  
  53.         parent::__construct();
  54.     }
  55.  
  56.     /**
  57.      * widget function.
  58.      *
  59.      * @see WP_Widget
  60.      *
  61.      * @param array $args
  62.      * @param array $instance
  63.      */
  64.     public function widget( $args, $instance ) {
  65.         if ( $this->get_cached_widget( $args ) ) {
  66.             return;
  67.         }
  68.  
  69.         global $product;
  70.  
  71.         $current_prod_id = $product->id;
  72.  
  73.         $current_product_cat = current(get_the_terms($current_prod_id, 'product_cat'))->term_id;
  74.  
  75.         $args['posts_per_page'] = !empty( $instance['number'] ) ? absint( $instance['number'] ) : '';
  76.         $args['post_status']    = 'publish';
  77.         $args['post_type']      = 'product';
  78.         unset($args['post_name']);
  79.  
  80.         $args['ignore_sticky_posts'] = 1;
  81.  
  82.         $args['meta_query'] = array(
  83.             array(
  84.                 'key'           => '_visibility',
  85.                 'value'         => array('catalog', 'visible'),
  86.                 'compare'       => 'IN'
  87.             )
  88.         );
  89.  
  90.         $args['tax_query'] = array(
  91.             'taxonomy'  => 'product_cat',
  92.             'field'     => 'term_id',
  93.             'terms'     => $current_product_cat,
  94.             'operator'  => 'IN'
  95.         );
  96.  
  97.         $args['orderby']  = 'date';
  98.  
  99.         $vals = new WP_Query( $args );
  100.  
  101.         echo "<pre>";
  102.         var_dump($vals->request);
  103.         echo "</pre>";
  104.  
  105.         ob_start();
  106.  
  107.         if ( ( $products = $vals ) && $products->have_posts() ) {
  108.             $this->widget_start( $args, $instance );
  109.  
  110.             echo apply_filters( 'before_products_by_currenct_cat_widget', '<ul class="product_list_widget">' );
  111.  
  112.             while ( $products->have_posts() ) {
  113.                 $products->the_post();
  114.                 $is_active = get_the_ID() == $current_prod_id?'product-active':'';
  115.                 wc_get_template( 'content-widget-product-custom.php', array( 'show_rating' => false, 'is_active'=>$is_active ) );
  116.             }
  117.  
  118.             echo apply_filters( 'after_products_by_currenct_cat_widget', '</ul>' );
  119.  
  120.             $this->widget_end( $args );
  121.         }
  122.  
  123.         wp_reset_postdata();
  124.  
  125.         echo $this->cache_widget( $args, ob_get_clean() );
  126.     }
  127.  
  128.      
  129. }
  130.  
  131. function register_products_by_current_cat_widget() {
  132.     register_widget( 'Products_By_Current_Cat_Widget' );
  133. }
  134.  
  135. add_action('widgets_init', 'register_products_by_current_cat_widget');
Advertisement
Add Comment
Please, Sign In to add comment