query_posts() and get_posts() eliminate conditional tags?

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)

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?

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.

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:

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().

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:

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.

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:

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: