How WordPress converts URL to $query_string

My home.php shows a list of (N) posts and << Previous & Next >> links. When clicked, I want to fetch previous/next list of (N) posts using Ajax. This is: I do NOT want to load the entire page. I just want to get the posts’ list and append it to current.

For this I make an ajax request and send href (URL) as parameter. The problem is I do not know how WordPress converts a URL to global $query_string. I want to reuse WordPress’ tested code instead of writing my own. Please help

For example: I want to fill $query_string for this URL: http://example.com/page/2/?location=boston

2 Answers
2

Check out WP::parse_request() – unfortunately, it was never really designed for public use. You could try this, not sure of the repercussions:

global $wp,
       $wp_the_query;

// Grab the request_uri component of the referer URL.
if ( ! $url = parse_url( $_GET['my_referer_parameter'] ) )
    return 'Oh dear, poorly formed referer.';

// Ewww! Trick WP::parse_request by spoofing REQUEST_URI. 
$_SERVER['REQUEST_URI'] = $url['path'] . "?{$url['query']}";

$wp->parse_request();
$wp->query_posts();

// $wp_the_query should now be populated with posts queried by your referer.
while ( $wp_the_query->have_posts() ) {

    $wp_the_query->the_post();
    the_title();

}

Leave a Comment