WordPress.org

Support

Support » How-To and Troubleshooting » Difference between these query methods?

Difference between these query methods?

  • I found this code format when looking to do some custom query work, before I did any research.

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		query_posts("paged=$paged&posts_per_page=-1");

    I have no idea what the top bit of code is doing, and while I get the posts per page bit, I don’t get what “paged=$paged” is for. Can anyone explain to me what this code is doing?

    I’ve now been using arrays for query parameter, like so:

    $args = array(
    						 'category_name' => '!featured',
    						 'offset' => 2,
    						 'posts_per_page' => 2,
    					);
    
    					query_posts($args);

    The latter seems to be the cleaner, more proper approach, but I’m not sure. If someone could tell me what that original code does exactly (specifically the paged bit), and if the array method is a more correct method, that would be great.

Viewing 2 replies - 1 through 2 (of 2 total)
  • catacaustic
    very awesome

    @catacaustic

    In your top example, $paged is getting a value from the query to see if the result is paged or not paged, and if there’s no value set, set it so that it is paged. The syntax is like:

    $var = if($cond) ? "val 1" : "val 2";

    That means… If $cond is true, set $var as “val 1” else set $var as “val 2”.

    Is it just an odd looking ternary statement? Perhaps it’s not even odd, I just haven’t used them in forever.

    As for something being “paged”, I’m not sure what that means in this context. I’ll give it a lookup now, but feel free to explain as well.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Difference between these query methods?’ is closed to new replies.