Discussion:
get wp_query object when you have url to post
Konrad Karpieszuk
2014-03-21 12:02:14 UTC
Permalink
hi

Imagine that you have some text (let it be a post content) with urls. I
want to take all urls (it is something what i know how to do), check if the
links are local (again, something easy) and if they are, check what exactly
should be queried from wordpress, what should be in wp_query.

Does wordpress has some easy function/methid to do this?

Like get_wp_query_by_url('http://example.com/archive/some-category-name')
which will return wp_query for 'some-category-name' archive page.

Another question, maybe more suitable for me: does wordpress has some easy
function/method which will return $_GET params which should be set when we
want to open this url?

Something like get_query_params_by_url('http://example.com/some-post');
which will return:

array(
'p' => 12
)

(params which normally will be added as $_GET to url if permalinks are set
to default)

Thank you in advance. Now everything what i do is to get url, slice it to
get its path and compare it with wp_rewrite. But because wordpress somehow
knows what it must display if somebody type some url in address bar i am
little affraid that there is some easier method than manual checking if url
matches to wp_rewrite rules

--
(en) regards / (pl) pozdrawiam
Konrad Karpieszuk
http://tradematik.pl wtyczka do WordPressa do tworzenia sklepów dla
klientów z Polski
Nikola Nikolov
2014-03-21 13:51:36 UTC
Permalink
Actually WordPress does the same thing internally(well it's a bit more
complicated, but it's about the same) - it goes through all rewrite rules
until it finds a matching regex pattern.

You might be able to run the method that generates the query, but I don't
know if it won't have a performance hit(especially if you'll be running it
for multiple links).

What is the reason behind why you need this functionality?

Nikola
Post by Konrad Karpieszuk
hi
Imagine that you have some text (let it be a post content) with urls. I
want to take all urls (it is something what i know how to do), check if the
links are local (again, something easy) and if they are, check what exactly
should be queried from wordpress, what should be in wp_query.
Does wordpress has some easy function/methid to do this?
Like get_wp_query_by_url('http://example.com/archive/some-category-name')
which will return wp_query for 'some-category-name' archive page.
Another question, maybe more suitable for me: does wordpress has some easy
function/method which will return $_GET params which should be set when we
want to open this url?
Something like get_query_params_by_url('http://example.com/some-post');
array(
'p' => 12
)
(params which normally will be added as $_GET to url if permalinks are set
to default)
Thank you in advance. Now everything what i do is to get url, slice it to
get its path and compare it with wp_rewrite. But because wordpress somehow
knows what it must display if somebody type some url in address bar i am
little affraid that there is some easier method than manual checking if url
matches to wp_rewrite rules
--
(en) regards / (pl) pozdrawiam
Konrad Karpieszuk
http://tradematik.pl wtyczka do WordPressa do tworzenia sklepów dla
klientów z Polski
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Konrad Karpieszuk
2014-03-21 14:03:42 UTC
Permalink
Actually WordPress does the same thing internally(well it's a bit more
Post by Nikola Nikolov
complicated, but it's about the same) - it goes through all rewrite rules
until it finds a matching regex pattern.
could you show me where it happen? which part of code?

i want to look on this and check how it is done and if it can inspire me
Post by Nikola Nikolov
What is the reason behind why you need this functionality?
Actually i don't need but i must ;) I work now on some legacy code, which
doesn't work correctly and is quite messy. I see only that it iterates
wp_rewrite rules and match links. I want to check if i can rewrite it in
better way. I tried to patch it but it looks now that it already has too
much patches which just fix one issue and do next one. Code has now about
500 lines and i feel that it is too much.
Nikola Nikolov
2014-03-21 14:24:56 UTC
Permalink
You can find the code in /wp-includes/class-wp.php WP::parse_request -
https://github.com/WordPress/WordPress/blob/3.8-branch/wp-includes/class-wp.php#L120


If you scroll down to line 199, you will see the foreach loop over the
rewrite rules.

Bad code written by other people is bad :)
Post by Nikola Nikolov
Actually WordPress does the same thing internally(well it's a bit more
Post by Nikola Nikolov
complicated, but it's about the same) - it goes through all rewrite rules
until it finds a matching regex pattern.
could you show me where it happen? which part of code?
i want to look on this and check how it is done and if it can inspire me
Post by Nikola Nikolov
What is the reason behind why you need this functionality?
Actually i don't need but i must ;) I work now on some legacy code, which
doesn't work correctly and is quite messy. I see only that it iterates
wp_rewrite rules and match links. I want to check if i can rewrite it in
better way. I tried to patch it but it looks now that it already has too
much patches which just fix one issue and do next one. Code has now about
500 lines and i feel that it is too much.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Konrad Karpieszuk
2014-03-21 14:49:53 UTC
Permalink
Thank you so much! :)


--
(en) regards / (pl) pozdrawiam
Konrad Karpieszuk
http://tradematik.pl wtyczka do WordPressa do tworzenia sklepów dla
klientów z Polski
Post by Nikola Nikolov
You can find the code in /wp-includes/class-wp.php WP::parse_request -
https://github.com/WordPress/WordPress/blob/3.8-branch/wp-includes/class-wp.php#L120
If you scroll down to line 199, you will see the foreach loop over the
rewrite rules.
Bad code written by other people is bad :)
Post by Nikola Nikolov
Actually WordPress does the same thing internally(well it's a bit more
Post by Nikola Nikolov
complicated, but it's about the same) - it goes through all rewrite
rules
Post by Nikola Nikolov
Post by Nikola Nikolov
until it finds a matching regex pattern.
could you show me where it happen? which part of code?
i want to look on this and check how it is done and if it can inspire me
Post by Nikola Nikolov
What is the reason behind why you need this functionality?
Actually i don't need but i must ;) I work now on some legacy code, which
doesn't work correctly and is quite messy. I see only that it iterates
wp_rewrite rules and match links. I want to check if i can rewrite it in
better way. I tried to patch it but it looks now that it already has too
much patches which just fix one issue and do next one. Code has now about
500 lines and i feel that it is too much.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...