Title: WP::build_query_string
Published: April 25, 2014
Last modified: August 20, 2026

---

# WP::build_query_string()

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#changelog)

Sets the query string property based off of the query variable property.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#description)󠁿

The [‘query_string’](https://developer.wordpress.org/reference/hooks/query_string/)
filter is deprecated, but still works. Plugins should use the [‘request’](https://developer.wordpress.org/reference/hooks/request/)
filter instead.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#source)󠁿

    ```php
    public function build_query_string() {
    	$this->query_string = '';

    	foreach ( (array) array_keys( $this->query_vars ) as $wpvar ) {
    		if ( '' !== $this->query_vars[ $wpvar ] ) {
    			$this->query_string .= ( strlen( $this->query_string ) < 1 ) ? '' : '&';

    			if ( ! is_scalar( $this->query_vars[ $wpvar ] ) ) { // Discard non-scalars.
    				continue;
    			}

    			$this->query_string .= $wpvar . '=' . rawurlencode( $this->query_vars[ $wpvar ] );
    		}
    	}

    	if ( has_filter( 'query_string' ) ) {  // Don't bother filtering and parsing if no plugins are hooked in.
    		/**
    		 * Filters the query string before parsing.
    		 *
    		 * @since 1.5.0
    		 * @deprecated 2.1.0 Use 'query_vars' or 'request' filters instead.
    		 *
    		 * @param string $query_string The query string to modify.
    		 */
    		$this->query_string = apply_filters_deprecated(
    			'query_string',
    			array( $this->query_string ),
    			'2.1.0',
    			'query_vars, request'
    		);

    		parse_str( $this->query_string, $this->query_vars );
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp.php#L609)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp.php#L609-L642)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#hooks)󠁿

 [apply_filters_deprecated( ‘query_string’, string $query_string )](https://developer.wordpress.org/reference/hooks/query_string/)

Filters the query string before parsing.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#related)󠁿

| Uses | Description | 
| [apply_filters_deprecated()](https://developer.wordpress.org/reference/functions/apply_filters_deprecated/)`wp-includes/plugin.php` |

Fires functions attached to a deprecated filter hook.

  | 
| [has_filter()](https://developer.wordpress.org/reference/functions/has_filter/)`wp-includes/plugin.php` |

Checks if any filter has been registered for a hook.

  |

| Used by | Description | 
| [WP::query_posts()](https://developer.wordpress.org/reference/classes/wp/query_posts/)`wp-includes/class-wp.php` |

Set up the Loop based on the query variables.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp/build_query_string/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.0.0](https://developer.wordpress.org/reference/since/2.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp%2Fbuild_query_string%2F)
before being able to contribute a note or feedback.