WordPress查询中0 = 1

时间:2017-02-20 07:46:50

标签: php wordpress

我按属性过滤WooCommerce产品:这是我正在使用的两个功能

public function get_the_posts( $posts, $query = false ) { 


            $filtered_posts   = array();
            $queried_post_ids = array();
            $query_filtered_posts = $this->layered_nav_query();

            foreach ( $posts as $post ) { 

                if ( in_array( $post->ID, $query_filtered_posts ) ) { 
                    $filtered_posts[]   = $post;
                    $queried_post_ids[] = $post->ID;
                }
            }

            $query->posts       = $filtered_posts;
            //print_r($query->posts );
            $query->post_count  = count( $filtered_posts );



            // Ensure filters are set
            $unfiltered_args = 
                array(
                    'post_type'              => 'product',
                    'numberposts'            => - 1,
                    'post_status'            => 'publish',
                    'fields'                 => 'ids',
                    'no_found_rows'          => true,
                    'update_post_meta_cache' => false,
                    'update_post_term_cache' => false,
                    'pagename'               => '',
                    'wc_query'               => 'get_products_in_view',
                    'suppress_filters'       => true

            );



            $this->unfiltered_product_ids = get_posts( $unfiltered_args );


            $this->filtered_product_ids   = $queried_post_ids;



            // Also store filtered posts ids...
            if ( sizeof( $queried_post_ids ) > 0 ) {
                $this->filtered_product_ids = array_intersect( $this->unfiltered_product_ids, $queried_post_ids );
            } else {
                $this->filtered_product_ids = $this->unfiltered_product_ids;
            }

            if ( sizeof( $this->layered_nav_post__in ) > 0 ) {
                $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
            } else {
                $this->layered_nav_product_ids = $this->unfiltered_product_ids;
            }

        echo $GLOBALS['wp_query']->request;
        //print_r($posts);
        return $posts;
    }



    public function layered_nav_query( $filtered_posts  = array() ) {
        global $_chosen_attributes, $woocommerce, $wp_query;

        if ( sizeof( $_chosen_attributes ) > 0 ) {

            $matched_products = array();
            $filtered_attribute = false;

            foreach ( $_chosen_attributes as $attribute => $data ) {

                $matched_products_from_attribute = array();
                $filtered = false;

                if ( sizeof( $data['terms'] ) > 0 ) {
                    foreach ( $data['terms'] as $value ) {

                        $posts = get_posts(
                            array(
                                'post_type'     => 'product',
                                'numberposts'   => -1,
                                'post_status'   => 'publish',
                                'fields'        => 'ids',
                                'no_found_rows' => true,
                                'tax_query' => array(
                                    array(
                                        'taxonomy'  => $attribute,
                                        'terms'     => $value,
                                        'field'     => 'id'
                                    )
                                )
                            )
                        );


                        if ( $data['query_type'] == 'or' ) {

                            if ( ! is_wp_error( $posts ) && ( sizeof( $matched_products_from_attribute ) > 0 || $filtered ) )
                                $matched_products_from_attribute = array_merge($posts, $matched_products_from_attribute);
                            elseif ( ! is_wp_error( $posts ) )
                                $matched_products_from_attribute = $posts;

                        } else {

                            if ( ! is_wp_error( $posts ) && ( sizeof( $matched_products_from_attribute ) > 0 || $filtered ) )
                                $matched_products_from_attribute = array_intersect($posts, $matched_products_from_attribute);
                            elseif ( ! is_wp_error( $posts ) )
                                $matched_products_from_attribute = $posts;
                        }

                        $filtered = true;

                    }
                }

                if ( sizeof( $matched_products ) > 0 || $filtered_attribute )
                    $matched_products = array_intersect( $matched_products_from_attribute, $matched_products );
                else
                    $matched_products = $matched_products_from_attribute;

                $filtered_attribute = true;

            }

            if ( $filtered ) {

                $woocommerce->query->layered_nav_post__in = $matched_products;
                $woocommerce->query->layered_nav_post__in[] = 0;

                if ( sizeof( $filtered_posts ) == 0 ) {
                    $filtered_posts = $matched_products;
                    $filtered_posts[] = 0;
                } else {
                    $filtered_posts = array_intersect( $filtered_posts, $matched_products );
                    $filtered_posts[] = 0;
                }

            }
        }



        return (array) $filtered_posts;
    }

add_filter('loop_shop_post_in', 'layered_nav_query');
if(!is_admin()) {
            add_filter( 'the_posts', 'get_the_posts', 15, 2 );
        }

但我没有结果:

以下是从WooCommerce生成的查询:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND wp_posts.ID IN (40,0) AND ( 0 = 1 ) AND ( ( wp_postmeta.meta_key = '_visibility' AND wp_postmeta.meta_value IN ('visible','catalog') ) ) AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC LIMIT 0, 12 

问题在这里:wp_posts.ID IN(40,0)AND(0 = 1)我检查过产品ID 40是否存在但在我的查询中添加0 = 1,我不明白。为什么呢?

任何人都可以帮助我吗?

0 个答案:

没有答案