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 How would you use these imaginary functions in conjunction with 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:03
  • Thanks @Wongjn, I've added some pseudocode. – Terence Eden Commented May 4, 2024 at 18:15
  • You could consider reviewing the source code of url_to_postid() and try to adjust the logic for your use case. – Wongjn Commented May 4, 2024 at 18:30
  • this sounds like a solution looking for a problem ;) don't think I ever saw a use of url_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
Add a comment  | 

1 Answer 1

Reset to default 1

The 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