WordPress.org

Ready to get started?Download WordPress

Forums

Question about query_post (8 posts)

  1. Tsalagi
    Member
    Posted 5 years ago #

    Am I understanding this code correctly? It really doesn't output anything it returns it and makes it available for elements in the loop to display.
    Is that correct? Can anyone point me to a well written article on the wp_query? It's all a bit confusing to me. Like how to style the query outputs and details like that. Does it belong in the loop? etc...

    <?php
    if(!$wp_query) global $wp_query;
    query_posts( array_merge( array('post_type'=>'page') , $wp_query->query ) );
    ?>

    Thanks in advance.

  2. Shane G.
    Member
    Posted 5 years ago #

  3. Tsalagi
    Member
    Posted 5 years ago #

    I'm aware of the codex. Thank you. I"m looking for a more down-to-earth breakdown.

  4. Mark / t31os
    Moderator
    Posted 5 years ago #

    <?php
    // If there's no $wp_query available..
    if(!$wp_query)
    
    // ... then globalise it, so we can read from it (this is just avoids having to catch any problem later, if it should occur (ie. the variable is outside the scope of where it's being called))
    global $wp_query;
    
    // Query posts defines parameters for "the loop" query, should you want to over-ride, change, remove or add any to it...
    query_posts( array_merge( array('post_type'=>'page') , $wp_query->query ) );
    
    ?>

    NOTE: query_posts, would typically go before the line if( have_posts() ) ... Ignore posts you see that place it after if( have_posts() ) as that's totally incorrect, as i've tried to point out before.. :)

    Array merge, joins 2 arrays, giving the first in the merge priority, if a matching array key exists in both arrays, the first array's key wins. In this case we're merging post_type into the existing queries arguments, if any post_type arg/parameter exists then it's overwritten by the new value, because it's in the first array.

    $wp_query->query

    Is a class variable (methods are functions), of the wp_query object ...

    Methods or variables inside an object (Google PHP OOP for more) are like associative arrays only there are some obvious differences (again see OOP related info). Namingly, you'll notice when you reference a variable (or key if you like) in an object, you use -> ito reference the item in the object..

    eg. When you see lines like.. $post->ID , it's referring to the ID variable inside the $post object..

    Each PHP function and WP function has an associated document (songdogtech provided one for WP_Query), but i don't think you'll make any sense of them without reading through each (it would be a little like explaining what a steering wheel is to someone who has no idea what a car is)... bad analogy aside, hope that helps..

    P.S. If you wanted a break down on the code, you could simply have asked in your other thread, i'd not be offended... ;) ... (just so you know)..

  5. Tsalagi
    Member
    Posted 5 years ago #

    t31os_. That was exactly what I was looking for.

    Each PHP function and WP function has an associated document (songdogtech provided one for WP_Query), but i don't think you'll make any sense of them without reading through each (it would be a little like explaining what a steering wheel is to someone who has no idea what a car is)... bad analogy aside, hope that helps..
    . As I am studying both WP and PHP I have been referring to both set of documents as I want all of the details I can get. Sometimes a breakdown like you provided makes the clouds part and lets the light of the code shine through.
    Thanks again.

  6. Mark / t31os
    Moderator
    Posted 5 years ago #

    Admittedly i've only really grasped using objects since i started writing and playing with code for WordPress, same is true for most of my PHP experience, though i've dabbled with it alot over the years on and off (followed the odd tutorial here and there etc..).

    Stick with it, it gets easier with time... ;) As with all things.. :)

  7. Tsalagi
    Member
    Posted 5 years ago #

    Thanks for the encouragement.

Topic Closed

This topic has been closed to new replies.

About this Topic