Title: build_query
Published: April 25, 2014
Last modified: February 24, 2026

---

# build_query( array $data ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#see-also)
 * [Parameters](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#wp--skip-link--target)

Builds a URL query based on an associative or indexed array.

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

This is a convenient function for easily building URL queries.
It sets the separator
to ‘&’ and uses the [_http_build_query()](https://developer.wordpress.org/reference/functions/_http_build_query/)
function.

### 󠀁[See also](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#see-also)󠁿

 * [_http_build_query()](https://developer.wordpress.org/reference/functions/_http_build_query/):
   Used to build the query

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#parameters)󠁿

 `$data`arrayrequired

URL-encode key/value pairs.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#return)󠁿

 string URL-encoded string.

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

    ```php
    function build_query( $data ) {
    	return _http_build_query( $data, null, '&', '', false );
    }
    ```

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

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

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

From php.net (modified by Mark Jaquith to behave like the native PHP5 function).

  |

| Used by | Description | 
| [WP_REST_Pattern_Directory_Controller::get_items()](https://developer.wordpress.org/reference/classes/wp_rest_pattern_directory_controller/get_items/)`wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php` |

Search and retrieve block patterns metadata

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

Retrieves a modified URL query string.

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

Creates the initial post types when ‘init’ action is fired.

  |

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

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

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 2 content](https://developer.wordpress.org/reference/functions/build_query/?output_format=md#comment-content-3341)
 2.    [Andy Schmidt](https://profiles.wordpress.org/andy-schmidt/)  [  7 years ago  ](https://developer.wordpress.org/reference/functions/build_query/#comment-3341)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbuild_query%2F%23comment-3341)
     Vote results for this note: 4[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbuild_query%2F%23comment-3341)
 4.  It’s not clearly spelled out that this function will call [_http_build_query()](https://developer.wordpress.org/reference/functions/_http_build_query/)
     with urlencode = FALSE. So it is assumed that you had previously urlencoded each
     individual key and value of your input array!
 5.  Crucially:
 6.      ```php
         $myarray = array( 'p%s/n#q?a*e!s p+' =>  'percent%slash/number#question?asterisk*exclamate!space plus+end' );
         build_query( $myarray );
         ```
     
 7.  will output:
      `p%s/n#q?a*e!s p+=percent%slash/number#question?asterisk*exclamate!
     space plus+end`
 8.  If you have an array with ‘raw’ data, you should use the native PHP function instead:
 9.      ```php
         $myarray = array( 'p%s/n#q?a*e!s p+' =>  'percent%slash/number#question?asterisk*exclamate!space plus+end' );
         http_build_query( $myarray );
         ```
     
 10. will output the proper/usable:
      `p%25s%2Fn%23q%3Fa%2Ae%21s+p%2B=percent%25slash%
     2Fnumber%23question%3Fasterisk%2Aexclamate%21space+plus%2Bend`
 11.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbuild_query%2F%3Freplytocom%3D3341%23feedback-editor-3341)

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