admin管理员组文章数量:1122832
If I do url_to_postid(";)
I get back an int which I can then pass to get_post()
. That works perfectly.
But what if I request a tag or a year? What's the equivalent which will let me turn or
/
into something I can use with have_posts()
?
For example, if someone visits example/2021/
in a web browser, the see the most recent 10 posts from that year. If they visit /tag/linux
, they see the most recent 10 posts with that tag.
At the moment, I can do
$id = url_to_postid( "; );
$post = get_post( $id );
// Do stuff with the post
Whereupon I can get the post's title, content, etc.
What I want to do is (pseudocode):
$posts = url_to_posts( "; );
while ( $posts->have_posts() ) : $posts->the_post();
echo get_the_content();
endwhile;
If I do url_to_postid("https://example.com/2024/05/test")
I get back an int which I can then pass to get_post()
. That works perfectly.
But what if I request a tag or a year? What's the equivalent which will let me turn https://example.com/tag/whatever
or https://example.com/2021/
into something I can use with have_posts()
?
For example, if someone visits example.com/2021/
in a web browser, the see the most recent 10 posts from that year. If they visit /tag/linux
, they see the most recent 10 posts with that tag.
At the moment, I can do
$id = url_to_postid( "https://example.com/2024/04/my-post" );
$post = get_post( $id );
// Do stuff with the post
Whereupon I can get the post's title, content, etc.
What I want to do is (pseudocode):
$posts = url_to_posts( "https://example.com/tag/linux" );
while ( $posts->have_posts() ) : $posts->the_post();
echo get_the_content();
endwhile;
Share
Improve this question
edited May 4, 2024 at 18:14
Terence Eden
asked May 4, 2024 at 16:49
Terence EdenTerence Eden
1577 bronze badges
4
|
1 Answer
Reset to default 1The simplest solution is to get all the existing rewrite rules, match the requested URl to one of them, and then convert it into a query.
Like so:
// Get all the rewrite rules
global $wp_rewrite;
$url_path = "2020/04/test-post"; // Or "tag/whatever" or "feed/atom" etc
// Match the URL against WordPress rewrite rules
$rewrite_rules = $wp_rewrite->wp_rewrite_rules();
$matched_rule = false;
foreach ( $rewrite_rules as $pattern => $query ) {
if ( preg_match( "#^$pattern#", $url_path, $matches ) ) {
$matched_rule = $query;
break;
}
}
// Replace each occurrence of $matches[N] with the corresponding value
foreach ( $matches as $key => $value ) {
$matched_rule = str_replace( "\$matches[{$key}]", $value, $matched_rule );
}
// Turn the query string into a WordPress query
$query_params = array();
parse_str(
parse_url( $matched_rule, PHP_URL_QUERY),
$query_params
);
// Construct a new WP_Query object using the extracted query parameters
$query = new WP_Query($query_params);
本文标签: wp queryEquivalent of urltopostid() for nonpost URLs
版权声明:本文标题:wp query - Equivalent of url_to_postid() for non-post URLs? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308281a1933605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
have_posts()
?have_posts()
does not accept any parameters and queries the global query, so I don't quite understand how you would intend to integrate these two things together. If you could provide some pseudo-code for how you would envisage you'd use these imaginary functions? What you would like returned by a year URL? – Wongjn Commented May 4, 2024 at 18:03url_to_postid()
and try to adjust the logic for your use case. – Wongjn Commented May 4, 2024 at 18:30url_to_postid
as you describe it therefor my gut feeling is that you have the wrong idea for solution to the problem you face. maybe you should explain what is that you want to do – Mark Kaplun Commented May 4, 2024 at 19:17