PHPackages                             gmazzap/url-to-query - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. gmazzap/url-to-query

ActiveWordpress-plugin

gmazzap/url-to-query
====================

Allow resolving any kind of WordPress url to related main query arguments.

1.0.1(9y ago)332.9k6[1 issues](https://github.com/gmazzap/Url_To_Query/issues)1MITPHP

Since Jul 17Pushed 9y ago4 watchersCompare

[ Source](https://github.com/gmazzap/Url_To_Query)[ Packagist](https://packagist.org/packages/gmazzap/url-to-query)[ Docs](https://github.com/Giuseppe-Mazzapica/Url_To_Query)[ RSS](/packages/gmazzap-url-to-query/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (1)

Url To Query
============

[](#url-to-query)

A WordPress plugin that allow resolving any kind of WordPress url (even from custom rewrite rules) to related main query arguments.

\#Requirements

- PHP 5.4+
- WordPress 3.9+
- [Composer](https://getcomposer.org/) to install

\#Installation

The plugin is a Composer package and can be installed in plugin directory via:

```
composer create-project gmazzap/url-to-query --no-dev
```

What &amp; Why
--------------

[](#what--why)

WP comes with a set of tools to convert custom urls into specific query variables: is possible to change the [permalink structure](http://codex.wordpress.org/Using_Permalinks#Choosing_your_permalink_structure), and there is also an [API](http://codex.wordpress.org/Rewrite_API/add_rewrite_rule) to add completely custom rules, however, there is **not** a way to *reverse* the process: i.e. to know to which query arguments an arbitrary url is connected to.

The url *resolving* is done in core by [`parse_request`](https://github.com/WordPress/WordPress/blob/71eb75a1599be8b456b2040f7ac2235c0e6b217e/wp-includes/class-wp.php#L120) method of `WP` class saved in the global `$wp` variable.

Using that method for the purpose explained above is hard/discouraged because:

- it directly accesses to `$_SERVER`, `$_POST` and `$_GET` variables, making hard to parse arbitrary urls not related with current HTTP request
- it triggers some action hooks strictly related to current HTTP request parsing, that makes no sense to trigger for arbitrary urls
- it accesses and modifies properties of global `$wp` variable that should not be changed after request is parsed or very likely *things* will break

This is the reason why I wrote this simple plugin, it adds a template tag **`url_to_query`** that accepts an url and returns related main query arguments.

\##How to use##

```
$args = url_to_query( 'http://example.com/sample-page' );
// $args = array(  'pagename' => 'sample-page' );

$args = url_to_query( 'http://example.com/category/uncategorized/' )
// $args = array(  'category_name' => 'uncategorized' );
```

It is also possible to pass a relative url:

```
$args = url_to_query( '/sample-page' );
// $args = array(  'pagename' => 'sample-page' );
```

\###Using query string###

When pretty permalinks are not used, (sometimes even in that case) WordPress can make use of query string in the urls to set query arguments. The plugin works perfectly with them:

```
$args = url_to_query( '/?attachment_id=880' );
$args = array(  'attachment_id' => '880' );
```

To simplify this task, `url_to_query` accepts a second argument: an array of query vars to be considered in the same way core considers `$_REQUEST` variables when an url is parsed:

```
$args = url_to_query( '/', array( 'attachment_id' => '880' ) );
// $args = array(  'attachment_id' => '880' );
```

Note that the array passed as second argument is not straight merged to the query vars, only valid query vars will be used, just like core does when parse urls:

```
$args = url_to_query( '/', array( 'attachment_id' => '880', 'foo' => 'bar' ) );
// $args = array(  'attachment_id' => '880' );
```

\###Custom rewrite rules###

Plugin works with no problems with custom rewrite rules, just few things to consider:

- `url_to_query` have to be called *after* query rules are added, or it can't recognize them
- just like for core, rewrite rules have to be flushed before `url_to_query` can recognize a newly added rule
- just like core, if the rule contains custom query variables, they have to be *allowed*, maybe using [`add_rewrite_tag`](http://codex.wordpress.org/Rewrite_API/add_rewrite_tag)or using `'query_vars'` filter (see [Codex example](http://codex.wordpress.org/Custom_Queries#Custom_Archives))

Example:

```
add_action( 'init', 'my_rew_rules' );

function my_rew_rules() {
  add_rewrite_tag('%food%', '([^&]+)');
  add_rewrite_tag('%variety%', '([^&]+)');
  add_rewrite_rule(
    '^nutrition/([^/]*)/([^/]*)/?',
    'index.php?page_id=12&food=$matches[1]&variety=$matches[2]',
    'top'
  );
}

add_action( 'footer' function() {
  $args = url_to_query( '/nutrition/cake/cherry/' )
  // $args = array( 'page_id' => '12', 'food' => 'cake', 'variety' => 'cherry' );
} );
```

\###Plugin classes###

Even if plugin provides the `'url_to_query'` template tag, it internally uses two classes to resolve urls and it is possible directly use them, instead of the template tag. Indeed, only one of them should be used, the second is used internally.

```
$resolver = new GM\UrlToQuery();
$args = $resolver->resolve( 'http://example.com/sample-page', array( 'page' => '2' ) );
// $args = array( 'pagename' => 'sample-page', 'page' => '2' );
```

So `resolve()` method of `GM\UrlToQuery` works exactly in the same way `url_to_query` function does.

The same instance of `GM\UrlToQuery` can be used to resolve different urls:

```
$resolver = new GM\UrlToQuery();

$args1 = $resolver->resolve( 'http://example.com/sample-page', array( 'page' => '2' ) );
// $args1 = array( 'pagename' => 'sample-page', 'page' => '2' );

$args2 = $resolver->resolve( '/?attachment_id=880' );
// $args2 = array( 'attachment_id' => '880' );

$args3 = $resolver->resolve( 'http://example.com/category/uncategorized/' );
// $args3 = array( 'category_name' => 'uncategorized' );
```

================

License
=======

[](#license)

Url\_To\_Query is released under MIT.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~436 days

Total

3

Last Release

3444d ago

Major Versions

v0.1.0 → 1.0.02014-10-30

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2208282?v=4)[Giuseppe Mazzapica](/maintainers/gmazzap)[@gmazzap](https://github.com/gmazzap)

---

Top Contributors

[![gmazzap](https://avatars.githubusercontent.com/u/2208282?v=4)](https://github.com/gmazzap "gmazzap (13 commits)")

---

Tags

wordpress

### Embed Badge

![Health badge](/badges/gmazzap-url-to-query/health.svg)

```
[![Health](https://phpackages.com/badges/gmazzap-url-to-query/health.svg)](https://phpackages.com/packages/gmazzap-url-to-query)
```

###  Alternatives

[roots/bedrock

WordPress boilerplate with Composer, easier configuration, and an improved folder structure

6.5k441.8k2](/packages/roots-bedrock)[humanmade/s3-uploads

WordPress plugin to store uploads on S3

2.1k2.4M9](/packages/humanmade-s3-uploads)[wp-media/wp-rocket

Performance optimization plugin for WordPress

7431.3M3](/packages/wp-media-wp-rocket)[10up/elasticpress

Supercharge WordPress with Elasticsearch.

1.3k374.3k6](/packages/10up-elasticpress)[roots/wp-stage-switcher

WordPress plugin that allows you to switch between different environments from the admin bar

382435.0k3](/packages/roots-wp-stage-switcher)[vinkla/wordplate

The WordPlate boilerplate

2.2k5.1k](/packages/vinkla-wordplate)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
