Url_To_Query icon indicating copy to clipboard operation
Url_To_Query copied to clipboard

This should seriously be merged into WP core

Open hybridwebdev opened this issue 5 years ago • 4 comments

Just wanted to pop in and give you a HUGE round of applause. This plugin tackles an issue i've been wracking my brains about for the better part of a week now. Your point about how parse_request is hard coded to use global variables, instead of parameters for the URL is spot on.

I realize you haven't touched the repo in 6 years, but it still works (albeit with a few tweaks). I will be forking it and submitting a pull request if you're interested in updating the repo.

Seriously though, thanks for providing this repo.

hybridwebdev avatar Jul 27 '20 12:07 hybridwebdev

Thanks @hybridwebdev

Sure, if you made this work with recent WP I'll be very glad to merge a PR.

gmazzap avatar Jul 28 '20 13:07 gmazzap

@hybridwebdev Appreciate your comment was last year, but what tweaks did you have to make to make, in order for it fit your needs? I've just been using this on a headless project to parse incoming paths back into the appropriate DB queries for REST endpoints and I'm starting to worry you might have found a gotcha I hadn't realised existed!

@gmazzap Awesome work with this! It's been a total life saver.

Penners avatar Sep 21 '21 23:09 Penners

@Penners There's a few caveats.

1 - It doesn't handle the site root correctly if you set your site to use page of posts. in this case, you will need to apply additional logic like so:

$query['page_id'] =  get_option( 'page_on_front' )

2 - If you want to use the array it returns there is some trapping you need to do as below:

$query  = url_to_query( $object ); 
        if(
            isset( $query['post_type'] ) && $query['post_type'] == $query['name']
        ){
            unset($query['name']);
        }

Those are the only edge cases I've come across. I also use it for a headless rest like system, and beyond those 2 caveats it's worked for the bulk of wordpress endpoints, including popular plugins like woo.

hybridwebdev avatar Sep 21 '21 23:09 hybridwebdev

@hybridwebdev Thanks for the follow up! it's good to hear that only the front page you found produces a weird result. My use case was getting the queried object like you would in a traditional WordPress request so this little bit of bodge worked for me. Your solution to get the front-page is definitely more elegant.

In case anyone finds it useful just posting the edge cases I handled for getting the queried object.

$page_slug =/= relative path (in this context)

$resolver = new UrlToQuery();
$params = $resolver->resolve($page_slug);

// something broke
if ($params instanceof \WP_Error) throw new HardException('Page "' . $page_slug . '" not found', 404);

$query = new \WP_Query($params);

// First method worked. Set Queried object and return
if ($query->get_queried_object()) {
    return $query->get_queried_object();
}

// This works in edge cases such as the front page weirdly
// cheap check to see if we can resolve url to post ID
$post_id = url_to_postid(get_site_url(null, $page_slug));
if ($post_id && is_numeric($post_id)) {
    return new \WP_Post(get_post($post_id));
}

$post_type = (is_array($params) && isset($params['post_type'])) ? get_post_type_object($params['post_type']) : null;
if ($post_type) {
    return $post_type;
}

throw new HardException('Page "' . $page_slug . '" not found', 404);

Penners avatar Sep 21 '21 23:09 Penners