WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_query (5 posts)

  1. richarduk
    Member
    Posted 5 years ago #

    I've been reading about multiple loops, and have been using query_posts up until now.

    So now I read here http://codex.wordpress.org/The_Loop that it's necessary to use wp_query, along the lines of

    <?php $first_loop = new WP_Query('showposts=200&cat=24&orderby=rand');
    while ($first_loop->have_posts()) : $first_loop->the_post(); 
    
    DO STUFF
    
    endwhile;

    The problem with this is that there's no if.. else, so that if there are no posts, an error message is displayed. (I want an error message if there are no posts)

    What do I do?

    Also, second question, it's not made clear here http://codex.wordpress.org/Function_Reference/WP_Query what parameters wp_query can take.

    Are they exactly the same parameters as query_post?

    Third question, query_post uses $query_string . to preserve information passed via a link. Is there an equivalent for wp_query?

    Many thanks

  2. MichaelH
    Member
    Posted 5 years ago #

    The problem with this is that there's no if

    Couldn't you use <?php if ($first_loop->have_posts()) : ?>

    Also, second question, it's not made clear here http://codex.wordpress.org/Function_Reference/WP_Query what parameters wp_query can take.

    Are they exactly the same parameters as query_post?

    Yes

    Third question, query_post uses $query_string . to preserve information passed via a link. Is there an equivalent for wp_query?

    Don't believe so, but there is $first_loop->query

  3. darrinb
    Member
    Posted 5 years ago #

    What you're looking for is something along the lines of this:

    <?php $first_query = new WP_Query('query terms in here');?>
    
    <?php if ($first_query->have_posts()) : while ($first_query->have_posts()) : $first_query->the_post(); ?>
    	DO STUFF
    	<?php endwhile; ?>
    	<?php else : ?>
    	DO STUFF IF NO POSTS
    <?php endif; ?>
  4. richarduk
    Member
    Posted 5 years ago #

    Thanks, MichaelH anddbmartin.

    I wasn't sure if if ...else was allowed (no reason why it shouldn't be, it just seemed rather definite in the article linked to above)

    Much appreciated to you both

  5. Mark / t31os
    Moderator
    Posted 5 years ago #

    If / Else is simply checking a condition, you can use it pretty much anywhere in PHP providing you use something valid to check..

    You can check if something exists, if something is equal to something else, if it's less than, more than..... and many many more things...

    If / Else is used in many scripting languages.... not just PHP.. and of course the syntax varies depending on the language...

Topic Closed

This topic has been closed to new replies.

About this Topic