Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- With widget below I get this query:
- SELECT wp_posts.* FROM wp_posts
- INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
- WHERE 1=1 AND wp_posts.post_name = 'sidebar'
- AND (
- ( wp_postmeta.meta_key = '_visibility'
- AND CAST(wp_postmeta.meta_value AS CHAR)
- IN ('catalog','visible')
- )
- )
- AND wp_posts.post_type = 'product'
- AND ((wp_posts.post_status = 'publish'))
- GROUP BY wp_posts.ID
- ORDER BY wp_posts.post_date DESC
- I want this query:
- SELECT wp_posts.* FROM wp_posts
- INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
- WHERE 1=1 AND ( ( wp_postmeta.meta_key = '_visibility'
- AND CAST(wp_postmeta.meta_value AS CHAR)
- IN ('catalog','visible')
- ) ) wp_posts.post_type = 'product'
- AND ((wp_posts.post_status = 'publish'))
- GROUP BY wp_posts.ID
- ORDER BY wp_posts.post_date DESC
- class Products_By_Current_Cat_Widget extends WC_Widget {
- /**
- * Constructor
- */
- public function __construct() {
- $this->widget_cssclass = 'woocommerce products_by_currenct_cat';
- $this->widget_description = __( 'Display a list of your products on current product page from its parent directory.', 'lauksva' );
- $this->widget_id = 'products_by_currenct_cat';
- $this->widget_name = __( 'Current Product Parent Category Products', 'lauksva' );
- $this->settings = array(
- 'title' => array(
- 'type' => 'text',
- 'std' => __( 'Products', 'lauksva' ),
- 'label' => __( 'Title', 'lauksva' )
- ),
- 'number' => array(
- 'type' => 'number',
- 'step' => 1,
- 'min' => 1,
- 'max' => '',
- 'std' => 5,
- 'label' => __( 'Number of products to show', 'lauksva' )
- ),
- );
- parent::__construct();
- }
- /**
- * widget function.
- *
- * @see WP_Widget
- *
- * @param array $args
- * @param array $instance
- */
- public function widget( $args, $instance ) {
- if ( $this->get_cached_widget( $args ) ) {
- return;
- }
- global $product;
- $current_prod_id = $product->id;
- $current_product_cat = current(get_the_terms($current_prod_id, 'product_cat'))->term_id;
- $args['posts_per_page'] = !empty( $instance['number'] ) ? absint( $instance['number'] ) : '';
- $args['post_status'] = 'publish';
- $args['post_type'] = 'product';
- unset($args['post_name']);
- $args['ignore_sticky_posts'] = 1;
- $args['meta_query'] = array(
- array(
- 'key' => '_visibility',
- 'value' => array('catalog', 'visible'),
- 'compare' => 'IN'
- )
- );
- $args['tax_query'] = array(
- 'taxonomy' => 'product_cat',
- 'field' => 'term_id',
- 'terms' => $current_product_cat,
- 'operator' => 'IN'
- );
- $args['orderby'] = 'date';
- $vals = new WP_Query( $args );
- echo "<pre>";
- var_dump($vals->request);
- echo "</pre>";
- ob_start();
- if ( ( $products = $vals ) && $products->have_posts() ) {
- $this->widget_start( $args, $instance );
- echo apply_filters( 'before_products_by_currenct_cat_widget', '<ul class="product_list_widget">' );
- while ( $products->have_posts() ) {
- $products->the_post();
- $is_active = get_the_ID() == $current_prod_id?'product-active':'';
- wc_get_template( 'content-widget-product-custom.php', array( 'show_rating' => false, 'is_active'=>$is_active ) );
- }
- echo apply_filters( 'after_products_by_currenct_cat_widget', '</ul>' );
- $this->widget_end( $args );
- }
- wp_reset_postdata();
- echo $this->cache_widget( $args, ob_get_clean() );
- }
- }
- function register_products_by_current_cat_widget() {
- register_widget( 'Products_By_Current_Cat_Widget' );
- }
- add_action('widgets_init', 'register_products_by_current_cat_widget');
Advertisement
Add Comment
Please, Sign In to add comment