Wallmander\ElasticsearchIndexer\Model\Query\WpConverter::isCompatible PHP Méthode

isCompatible() public static méthode

public static isCompatible ( Wp_Query $wpQuery )
$wpQuery Wp_Query
    public static function isCompatible(Wp_Query $wpQuery)
    {
        $q = $wpQuery->query_vars;
        $unsupportedQueryArgs = ['suppress_filters', 'has_password', 'post_password', 'preview', 'fields'];
        foreach ($q as $key => $value) {
            if ($value && in_array($key, $unsupportedQueryArgs)) {
                return false;
            }
        }
        if ($q['fields'] == 'ids' || $q['fields'] == 'id=>parent') {
            return false;
        }
        if (!empty($q['post_status'])) {
            if (is_string($q['post_status'])) {
                $q['post_status'] = explode(' ', str_replace(',', ' ', $q['post_status']));
            }
            $ips = Indexer::getIndexablePostStati();
            foreach ($q['post_status'] as $value) {
                if (!in_array($value, $ips)) {
                    return false;
                }
            }
        }
        if (!empty($q['post_type']) && $q['post_type'] !== 'any') {
            if (is_string($q['post_type'])) {
                $q['post_type'] = explode(' ', str_replace(',', ' ', $q['post_type']));
            }
            $ipt = Indexer::getIndexablePostTypes();
            foreach ($q['post_type'] as $value) {
                if (!in_array($value, $ipt)) {
                    return false;
                }
            }
        }
        return true;
    }

Usage Example

 /**
  * Filter query string used for get_posts(). Search for posts and save for later.
  * Return a query that will return nothing.
  *
  * @param string    $request
  * @param \WP_Query $query
  *
  * @return string
  */
 public static function filterPostsRequest($request, WP_Query $query)
 {
     if (apply_filters('esi_skip_query_integration', false, $query)) {
         return $request;
     }
     if (!WpConverter::isCompatible($query)) {
         $query->is_elasticsearch_compatible = false;
         return $request;
     }
     $query->is_elasticsearch_compatible = true;
     global $wpdb;
     return "SELECT * FROM {$wpdb->posts} WHERE 1=0";
 }