Wallmander\ElasticsearchIndexer\Model\Query\WpConverter::formatArgs PHP Method

formatArgs() public static method

public static formatArgs ( Query $query, Wp_Query $wpQuery )
$query Wallmander\ElasticsearchIndexer\Model\Query
$wpQuery Wp_Query
    public static function formatArgs(Query $query, Wp_Query $wpQuery)
    {
        // Fill again in case pre_get_posts unset some vars.
        $q = $wpQuery->fill_query_vars($wpQuery->query_vars);
        $q = apply_filters('esi_before_format_args', $q, $query);
        if ($wpQuery->is_posts_page) {
            $q['pagename'] = '';
        }
        // Defaults
        if (!empty($q['nopaging'])) {
            $q['posts_per_page'] = '-1';
        }
        if (empty($q['posts_per_page'])) {
            $q['posts_per_page'] = get_option('posts_per_page');
        }
        if (empty($q['post_type'])) {
            if (!empty($q['wc_query']) && $q['wc_query'] == 'product_query') {
                $q['post_type'] = 'product';
            } elseif (!empty($q['tax_query'])) {
                $q['post_type'] = 'any';
            } else {
                $q['post_type'] = 'post';
            }
        } elseif (is_string($q['post_type'])) {
            $q['post_type'] = explode(' ', str_replace(',', ' ', $q['post_type']));
        }
        if (empty($q['orderby'])) {
            $q['orderby'] = 'none';
        }
        if ($wpQuery->is_search() && $wpQuery->get('s')) {
            if ($q['orderby'] == 'none' || $q['orderby'] == 'menu_order title') {
                $q['orderby'] = 'relevance';
            }
        }
        if (empty($q['post_status'])) {
            $q['post_status'] = ['publish'];
            if (is_user_logged_in()) {
                $q['post_status'][] = 'private';
            }
            if (is_admin()) {
                $q['post_status'][] = 'future';
                $q['post_status'][] = 'draft';
                $q['post_status'][] = 'pending';
            }
        } elseif (is_string($q['post_status'])) {
            $q['post_status'] = explode(' ', str_replace(',', ' ', $q['post_status']));
        }
        $q = apply_filters('esi_before_query_building', $q, $query);
        // Loop all query arguments
        foreach ($q as $key => $value) {
            if (!$value) {
                continue;
            }
            $f = 'arg' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
            $c = get_class();
            if (method_exists($c, $f)) {
                $c::$f($query, $q[$key], $q);
            }
        }
        do_action('esi_after_format_args', $query, $wpQuery);
    }

Usage Example

 public function formatArgs(WP_Query $wpQuery)
 {
     WpConverter::formatArgs($this, $wpQuery);
     return $this;
 }