WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query_posts() and get_posts() eliminate conditional tags? (16 posts)

  1. karimun
    Member
    Posted 6 years ago #

    I was looking for a way to display page specific content in the sidebar. I had the problem that placing query_posts() or get_posts() before the loop in my page templates it breaks the execution of conditional tags like is_page('8') in the sidebar template:

    THE FOLLOWING SCENARIO DOES NOT WORK:
    (values of page-id, page-title or page-slug are all ZERO in sidebar.php)

    Code in my-page-template.php

    <?php query_posts("cat=any_cat_id"); ?>
    <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
    My post listing.
    <?php endwhile; ?><?php else : ?>No posts.<?php endif; ?>

    Code in sidebar.php

    <?php if (is_page('any_page_id')){ ?>
    Content in sidebar for pageID=8
    <?php } ?>

    THE FOLLOWING SCENARIO WORKS:
    (..in my case. The solution was to work with sessions)

    Code in my-page-template.php

    <?php
    //this line before ANY query_posts() or get_posts() queries and loopings!
    $page_id = $wp_query->get_queried_object_id(); $_SESSION["pageid"] = $page_id;
    ?>
    ... query_posts() and loop

    Code in sidebar.php

    if ($_SESSION["pageid"] == any_page_id){ echo "This is page with ID=any_page_id";}

    Question:
    Can a moderator or other experienced users of this forum confirm that using query_posts() or get_posts() in a page template breaks conditional tags in the sidebar with WP3.2.X ? What do I need to consider when using the working code above in terms of restrictions?

  2. jleez
    Member
    Posted 6 years ago #

    Just wanted to chime in and say that I'm having a very similar problem too. I'm trying to put in a dynamic sidebar into my site, and it's not working. I traced the problem back to my "Latest Posts" section of the theme's leftmost static sidebar, which uses query_posts to call the latest 10 entries.

    I placed a snippet of code in my dynamic sidebar that would spit out the words "This is home!" whenever is_home was true. With the query_posts command in the static sidebar, I had the words "This is home!" on every single web page. However, if I removed the query_posts command, is_home was no longer true for every single page.

    I only have very basic knowledge of PHP, so I don't know how to solve this problem. For right now, the only solution I can think of is removing my "Latest Posts" section.

    (I am running 2.3.3 as well, if that matters at all.)

  3. karimun
    Member
    Posted 6 years ago #

    jleez, just try my $_SESSION[] example. In your case maybe you have to play around with the $wp_query.
    Good luck.

  4. Kafkaesqui

    Posted 6 years ago #

    The use of query_posts() *can* reset the values of conditional properties in the default query object ($wp_query). This is because of how query_posts() goes about its job. For example:

    <?php query_posts("cat=any_cat_id"): ?>

    is not a simple database query collecting posts from a specific category, but is resetting the query object as a whole to reflect a normal category query, as if one is on a category's page(s). You can try something like:

    <?php query_posts($query_string . "&cat=any_cat_id"): ?>

    to try and retain the original properties of the query object for anything you're not resetting with your custom query. Another option is to manually set values of the various query object properties *after* query_posts() has run:

    <?php
    query_posts("cat=any_cat_id"):
    $wp_query->is_category = false;
    $wp_query->is_home = true;
    ?>

    (The two solutions above can also be combined.)

    Finally, to completely avoid the default query object on a page, you can create a new WP_Query class object and work off of that in your posts loop:

    <?php $my_posts = new WP_Query("cat=any_cat_id"): ?>
    <?php while( $my_posts->have_posts(); $my_posts->the_post(); ?>

    Note: get_posts() does not touch the default query object in any way *except* when you assign it to $posts, which is the variable name for the default posts object. To avoid that, follow the example above with WP_Query and use a different var name (i.e. $my_posts).

    EDIT: Following up with a couple more points:

    1. There's a function inherently tied to The Loop called rewind_posts() which when used after a custom loop *should* reset everything back.

    2. I want to emphasize that on pages with multiple post loops, the best option is to set up a new posts query through WP_Query for additional posts loops, or to use get_posts().

  5. karimun
    Member
    Posted 6 years ago #

    Thank you for your detailed answer.
    Unfortunately in my case your suggestions dont help. CONDITIONAL TAGS in the SIDEBAR TEMPLATE are not processed if I implement query_posts() in the page main template:

    is_page() | is_page(8) | is_page('test-page') | is_page('Test page') wont work.

  6. Kafkaesqui

    Posted 6 years ago #

    Try:

    <?php
    query_posts("cat=any_cat_id"):
    $wp_query->is_category = false;
    $wp_query->is_page = true;
    ?>
  7. karimun
    Member
    Posted 6 years ago #

    still nothing.

    Page has ID 85.
    My code in the page template (note, its not directly in page.php, template is set in the admin panel under "Page template"):

    <?php query_posts("cat=4&showposts=-1&orderby=title&order=asc");
    
    $wp_query->is_category = false;
    $wp_query->is_page = true;
    ...
    ?>

    My code in the sidebar template:

    if (is_page(85)){echo "sidebar content for page 85";}

    .. nothing displays in the sidebar.

  8. Kafkaesqui

    Posted 6 years ago #

    Then (as noted above) you'll have to go with creating a new query object through WP_Query() and work off that:

    <?php $cat_posts = new WP_Query("cat=any_cat_id"); ?>
    <?php if($cat_posts->have_posts()) : ?><?php while($cat_posts->have_posts()) : $cat_posts->the_post(); ?>

    This will leave $wp_query alone, and all normal conditional tests should work as expected.

  9. karimun
    Member
    Posted 6 years ago #

    Perfect! It works now. Thank you for your assistance.

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Using wp_reset_query(); would also work to reset the query back to the way it's supposed to be.

    However, creating an entirely new WP_Query object is the preferred solution.

  11. Bonnie
    Member
    Posted 6 years ago #

    Ahhhhh!

    I would like to thank karimun for posting this thread, Kafkaesqui for providing a solution, and Otto42 for providing an alternative solution. This problem has been giving me a headache all afternoon, as I run The Loop a few times in my sidebar and it was breaking my main query. Creating new WP_Query objects for the sidebar has fixed it.

    THANK YOU!

  12. eknekn
    Member
    Posted 6 years ago #

    I would like to second this thank you. This solution has also worked perfectly for me, so far. Now I'm wondering if I can make this more efficient...

    Using page name variable to show a category of posts

    I also dynamic sidebar that changes depending on the page. I'm creating categories so that there is a category name that corresponds with each page slug, and then using "if is_page" to show the posts in the related category.

    For example:
    page slug = about
    post category name = about

    Since I have over 20 pages, I would rather not have 20+ if then statements. I would rather figure out a way to have one if-then statement that places the current page slug into both the the IF IS_PAGE, part of the statement and the new WP_Query("category_name=about").

    I am new to php and database programming as a whole, but it seems like there would be a logical way to do this by creating a variable and putting it in both parts of the query. I've seen a few posts on the forum that are along the same lines, but I can't figure it out.

    Basically, automate the if then statement below, so that I only write it once:

    if (is_HOME()) {
    
     $my_query = new WP_Query("category_name=HOME");
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;
     the_title();
      the_content('More...');
       endwhile;
    }   elseif (is_page()) {
    
            if (is_page('ABOUT')) {
    
     $my_query = new WP_Query("category_name=ABOUT");
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;
    the_title('<h3>', '</h3>');
      the_content('Read more about this client & others&raquo;');
       endwhile;

    etc...

    Thanks in advance for your help!

  13. moshu
    Member
    Posted 6 years ago #

    Using page name variable to show a category of posts

    Why are you using a Page to show posts in a category, instead of using the WP's built-in solution, the Category_Templates? Just curious...

  14. MiOtraCosa
    Member
    Posted 6 years ago #

    Using page name variable to show a category of posts

    Why are you using a Page to show posts in a category, instead of using the WP's built-in solution, the Category_Templates? Just curious...

    As one who has done the exact same thing I can provide some valid reasons:

    Sometimes I ask myself this same question when I find myself struggling.

    Hey eknekn, if you are still out there, please update on your requirements. Sounded great.

  15. MiOtraCosa
    Member
    Posted 6 years ago #

    Kafkaesqui, your solution is awesome, very clean! Many thanks.

    I wish that all suggestions to use <?php query_posts("cat=any_cat_id"); ?> could be removed from the archives to spare anyone else these problems.

    Now I'm back in action with using Daiko and Widget Logic.

  16. rocketman1223
    Member
    Posted 5 years ago #

    i think my issue is close enough to this topic to add to this thread...

    I want a sidebar pop-up to pull content from portfolio pages and then link to them in the sidebar (a good example would be the "Recent Projects" section on http://www.barbariangroup.com/). To do so, I will need to pull permalink, title, tags and likely a custom field or two. But, all of this is about 2 steps ahead of where I am right now.

    Using the following very basic test query, I'm only able to pull permalink and title. I'm not close to adequate with PHP to figure out my problem and this is my first stab at making significant customizations in WP (despite using it for years). Here's my code:

    <?php $portfolio_posts = new WP_Query('page_id=1156');
    if($portfolio_posts->have_posts()) : while($portfolio_posts->have_posts()) : $portfolio_posts->the_post();
    ?>
      <!-- do stuff -->
    <?php endwhile;
    endif; ?>

    From a different forum, I got the idea to try the following, which does pull the correct content from the page, but it completely ruins all other post queries on the page.

    <?php global $post;
    $myposts = get_posts('page_id=1156');
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
      <!-- do stuff -->
    <?php endwhile; ?>

    Thanks in advance!

Topic Closed

This topic has been closed to new replies.

About this Topic