• Resolved filchos

    (@filchos)


    Hello,

    while writing a high level interface to WordPress for usage in the templates I ran across a strange problem: Consider the following file page.php:

    <?php
    
    global $wp_query;
    
    $current = $wp_query;
    var_dump($current->request);
    
    $new = new WP_Query($wp_query->query_vars);
    var_dump($new->request);
    
    var_dump(array_diff_assoc($current->query_vars, $new->query_vars));

    When I open a single page, say with id 5 and slug foobar, I’ll get the same query vars (tested with array_diff_assoc, as well as manually), but two different SQL requests:

    // $current:
    string 'SELECT   wps13t_posts.* FROM wps13t_posts  WHERE 1=1  AND (wps13t_posts.ID = '5') AND wps13t_posts.post_type = 'page'  ORDER BY wps13t_posts.post_date DESC '
    
    // $new:
    
    string 'SELECT   wps13t_posts.* FROM wps13t_posts  WHERE 1=1  AND wps13t_posts.post_name = 'foobar' AND wps13t_posts.post_type = 'post'  ORDER BY wps13t_posts.post_date DESC '

    The first SQL request uses the page id (correct) and post_type post (correct). The 2nd SQL request uses the post_name (correct, too) and post_type page (error!).

    I’m wondering why this strange behaviour occurs and whether it’s me or WordPress itself causing this error.

    How can I init a WP_Query that acts the same as the default query?

    Thanks for your help,
    /Olaf

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s improper to directly pass query vars as WP_Query construction arguments. While there is a great deal of similarity between the two, some translation is applied so that the resulting request can often be different.

    For example, for the main query, when the queried object is determined by the page slug (from getting a page by permalink), it causes the post type to be specified as ‘page’ in the request. But when you pass 'post_type'=>"" (from query vars) to a new WP_Query instance, the default ‘post’ value is assumed in the resulting request.

    Thread Starter filchos

    (@filchos)

    Thank you for your help, bcworkz!

    Sometimes WordPress is hard to understand since it’s kind of quaint sometimes.

    Just one case:

    query_vars of the current main loop contain both name and pagename without any problem in the main loop.

    If I set these vars in the constructor of an own request I would expect the same result. But post_type will be set to post instead. Only when I remove the name var it works as excepted. I had similar cases with the vars p and page_id causing the same problem.

    What I don’t understand: The main loop uses a Wp_Query instance as well (the global wp_query), but when I try to mimic the behaviour by constructing a Wp_Query instance that looks alike to me I get a different behaviour. Somehow the main loop must have another initialisation process as my code, but I didn’t find a clue, neither in the WordPress documentation, nor by checking the code itself.

    There are heaps of loop and Wp_Query related functions and methods initialising, setting up, building objects, destroy object, reset the pointer and so one but I didn’t come up to a working combination.

    Moderator bcworkz

    (@bcworkz)

    “kind of quaint” 😀 That’s a very judicious way of putting it!

    While the main query does use an instance of WP_Query, it is not used in the way you think. The main query’s query vars are from WP::query_vars which are established by WP::parse_request(). The WP_Query::query_vars you are using are really just a side effect for the main query. Note the two different classes here, WP and WP_Query, the distinction is important.

    When you instantiate your own WP_Query object, the WP_Query::query_vars are quite important, they are established from the passed arguments with WP_Query::parse_query(), which is similar but different from the WP::parse_request() used by the main query. This is how ‘page’ gets worked into one request but not the other.

    Thread Starter filchos

    (@filchos)

    Thank you for your explanations, bcworkz!

    Now I understand better twice: How it works and how awkward WordPress’ internals are.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Same query_vars, but different SQL request between main loop and Wp_Query’ is closed to new replies.