douxiang3978
douxiang3978
2015-04-06 01:30

从所有自定义字段搜索非常慢。 我需要过滤搜索自定义帖子类型

  • database
  • wordpress
  • php
  • mysql

I'm doing a custom search in my WP and in the search field, any typed word will be sought in custom-fields, in the titles and posting content.

It turns out that my database has more than 1 million custom-fields lines, and wanted to limit the search only for a certain type of post. I already do this in the Loop to display the results, but I wish this filter was made in consultation because the page is taking over 20 seconds to display the result.

Can anyone help me? Below the code I'm using.

    // SEARCH FROM ALL CUSTOM FIELDS
    $post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
    ALTER TABLE wp_posts ADD INDEX (postmeta)
    SELECT DISTINCT post_id FROM {$wpdb->postmeta}
    WHERE meta_value LIKE '%s'
    ", $keyword ) );

    // SEARCH FOR TITLE AND CONTENT
    $post_ids_post = $wpdb->get_col( $wpdb->prepare( "
    ALTER TABLE wp_post s ADD INDEX (posts);
    SELECT DISTINCT ID FROM {$wpdb->posts}
    WHERE post_title LIKE '%s'
    OR post_content LIKE '%s'
    ", $keyword, $keyword ) );
    $post_ids = array_merge( $post_ids_meta, $post_ids_post );
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答