WordPress.org

Ready to get started?Download WordPress

Forums

query posts (37 posts)

  1. Root
    Member
    Posted 9 years ago #

    Does anyone happen to know where an expo on query_posts is to be found please. On Codex it is descibed as coming shortly. (1.5).

  2. ifelse
    Member
    Posted 9 years ago #

    query_posts allows you to reuse the $wp_query object. By passing in parameters to query_posts, that query will be performed.

    This will reset the loop counter and rebuild the collection of posts allowing you to go back through the_loop again.

  3. ifelse
    Member
    Posted 9 years ago #

    By parameters, I mean you would call query_posts as such:
    query_posts('category_name=your_category&showposts=10');

  4. Root
    Member
    Posted 9 years ago #

    Too late. I got it fixed up. Thanks for readiong this far. There is an example of it in action on Codex.

  5. ifelse
    Member
    Posted 9 years ago #

    Ah crap, I remembered seeing it somewhere. For future reference, see here.

  6. Kafkaesqui

    Posted 9 years ago #

    I *really* have to get to work on that query_posts page...

  7. Root
    Member
    Posted 9 years ago #

    I still havent quite got the syntax. Does the query go inside the loop. Then the rewind ?

  8. Kafkaesqui

    Posted 9 years ago #

    Use it just before (but not in) the loop it's to act upon.

  9. Root
    Member
    Posted 9 years ago #

    Then rewind? I ask because it isnt running smoothly. I have tried most combos.
    Or is the rewind built into the query as it seems ? In the Codex example the query sits first but then the loop only executes the query.

  10. ifelse
    Member
    Posted 9 years ago #

    You shouldn't need to rewind. The loop counter is reset upon execution so it acts as an implicit rewind.

  11. Root
    Member
    Posted 9 years ago #

    Ah cool. But then the loop wont execute.

  12. Kafkaesqui

    Posted 9 years ago #

    rewind is somewhat unrelated to query_posts. Depending on what you're trying to do, an option may be to reinitialize the default loop through query_posts('showposts').

  13. ifelse
    Member
    Posted 9 years ago #

    Here's roughly what you would be doing:
    if (have_posts()) : while (have_posts()) : the_post();
    the_title();
    endwhile;
    //now run a new query
    query_posts('category_name=site&showposts=10');
    //as $wp_query is reinitialise, let's run the_loop
    while (have_posts()) : the_post();
    the_title();
    endwhile;
    endif;

  14. Root
    Member
    Posted 9 years ago #

    This looks good. TY.

  15. lawtai
    Member
    Posted 9 years ago #

    The thing with the query post is that it bypasses any password protection if you're using PW's. Know of a way to still include the PW protection?

  16. Kafkaesqui

    Posted 9 years ago #

    lawtai: Try reusing the code for that from the_content() and perform an if/else just after the start of your custom loop:

    <?php if(!empty($post->post_password)) :
    if(stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { // and it doesn't match the cookie
    $output = get_the_password_form();
    echo $output;
    }
    else : ?>

    ~drop your regular loop stuff here ~

    <? endif; ?>

    Don't know if it'll work, but worth a shot.

  17. lawtai
    Member
    Posted 9 years ago #

    This is what I have for the "blog.php" file which is basically the index but on the blog template so that there could be a home page.


    <?php if(!empty($post->post_password)) :
    if(stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { // and it doesn't match the cookie
    $output = get_the_password_form();
    echo $output;
    }
    else : ?>

    <?php query_posts("showposts=10"); ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    ~The rest of my loop~

    I end up getting this error:Parse error: parse error, unexpected ':' in /home/.nabbenna/yogaboat/cottonmill.info/wp-content/themes/daisyraegemini/blog.php on line 15

    Any suggestions?

  18. Root
    Member
    Posted 9 years ago #

    Well there is a period just before nabbenna ?

  19. Kafkaesqui

    Posted 9 years ago #

    "Any suggestions?"

    Without seeing the whole thing...do you have the closing endif in there after your normal loop content?

  20. lawtai
    Member
    Posted 9 years ago #

    yes, I do have the closing endif.

    http://paste.uni.cc/6853

    That's the current code for the blog.php file. Since it's considered a "page" by WP, and I'm trying to have it show what the index normally would, I want to be able to PW protect the whole page. Hopefully you understand what I'm trying to get at.

    That's without your code.

    http://paste.uni.cc/6854

    -with your code

  21. Kafkaesqui

    Posted 9 years ago #

    Hmm. You've hit a limitation to page templating. Or rather, password protection.

    When you set a password on a post or Page, you're protecting the content of that post, and not an actual document. If you were to run a regular iteration of The Loop before your custom post query, you'd see your Page is password protected--but only the contents of your Page, and not the page.

    Ok, possible solution:

    http://paste.uni.cc/6855

    I've inserted a simplified loop to grab the Page's password, and use it for your custom post query verification.

    Note: I commented out your gravatar.php include, but forgot to uncomment it in the paste up.

  22. Root
    Member
    Posted 9 years ago #

    I am still not quite getting this. Is it mandatory that the query runs inside the loop?

  23. lawtai
    Member
    Posted 9 years ago #

    Thanks Kafkaesqui! That worked great!

    Root: The query should run justbefore the loop from what I understand. But by doing so, you basically force your loop to show the query, therefore bypassing any password protection you may be using.

  24. Root
    Member
    Posted 9 years ago #

    Well it doesnt work :)

  25. yogaboat
    Member
    Posted 9 years ago #

    Works for me! I'd like to thank lawtai for his persistence with this issue. :D

  26. Root
    Member
    Posted 9 years ago #

    Mine is up too. Thanks guys and to Nick at MtDewVirus.

  27. eyetag
    Member
    Posted 9 years ago #

    kafkaesqui, installed your password query and combined it as suggested with individual categories. this works also for what i need, besides of two things:

    1) when entering the protected category by pw it shows all entries instead of only those from the protected one
    2) i would like to exclude the entries (headers etc.) of the protected category from the mainpage

    you can try it at http://www.eyetag.de/weblog
    pw: test

    for what reason do i have to put it into its own category template?
    you could simply leave in index.php, couldn´t you?

    thanks&cheers

  28. eyetag
    Member
    Posted 9 years ago #

    ANYTHING WRONG WITH MY REQUEST?

    guys, please help me out of this!
    can´t get further without your help! i´m really wondering why some posts get ignored sometimes (like those on this topic)...
    is it only because there´s no quick solution to it? if there´s anything i can do better in my problems description or anythng else to improve feedback rate, please let me know!

    again: thanks in advance!
    et

  29. chrisgalfi
    Member
    Posted 9 years ago #

    I'm trying to display a page at the top of my home page and the posts as normal below. However, the navigation links disappear in almost any combination of adding a Page to the top of the home page (I've tried plugins and my own variations).

    I believe the problem is from calling the loop twice in the same page, which is not getting reset properly the second time thorugh. I even tried using my own WP query object. I did notice that the WP query object refers to a global post on the "the_post()" function. The use of globals in instance variables is what leads me to believe something isn't getting reset properly.

    This is my first time into the guts of WordPress, and I haven't gone too far to debug the problem. Before I do, I wanted to see if anyone else is seeing the same problem.

  30. Root
    Member
    Posted 9 years ago #

    This is two problems mixed together. Putting a *page* at the top is not quite the same as having a sticky or an initial query. Could you post your loop code?

Topic Closed

This topic has been closed to new replies.

About this Topic