wordpress自定义查询太慢了

时间:2018-07-12 14:25:16

标签: php mysql wordpress woocommerce

我正在尝试使用woocommerce中的自定义表创建自定义过滤器。大约有24000种产品使自定义过滤器变得非常慢。如何使其更快?

这是我的代码:

function custom_wpquery( $query ){
    if(isset($_GET['min-price'])){
    // the main query
    global $wp_the_query;
 global $wpdb;
    if ( 'product' === $query->get( 'post_type' ) ) {

        $rr = g($_GET['min-price'],$_GET['max-price']);

        foreach($rr as $rrr){

           $fabricc = $wpdb->get_results("SELECT * FROM CWF_posts WHERE post_title = '".$rrr."' AND post_type = 'product'");

           foreach($fabricc as $fabriccc){
        $cID[] = $fabriccc->ID;    
           }

        }

        //print_r();exit;
      //  foreach($cID as $cIDD){
          //  $cat= wp_get_post_terms( $dd->ID, 'product_cat' );
       // }
          //$dd = get_page_by_title( $rrr, OBJECT, 'product' );

         //   $include[]=$dd->ID;
           // 
           // $c[] = $cat[0]->slug;
            //$c2[] = $cat[1]->slug;
       $query->set('post__in', $cID); 
    }
    }
}
add_filter( 'pre_get_posts', 'custom_wpquery' );

谢谢

1 个答案:

答案 0 :(得分:1)

您可以在要查询的字段上创建多重索引...由于您的一个字段是常量,因此我建议您将SQL转换为

SELECT * FROM CWF_posts WHERE post_type = 'product' AND post_title = '".$rrr."'

并在MySQL上使用此命令在 post_type post_title 上创建索引

CREATE INDEX type_title_index on CWF_posts (post_type, post_title)

您还可以检查

Understanding multiple column indexes in MySQL query

https://dev.mysql.com/doc/refman/8.0/en/create-index.html