WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having trouble running query_posts when my page loads for the first time, i.e Session is empty.

When the page loads for the first time and the SESSION is empty, the following code runs:

    $id = 106;
  if ((empty($_SESSION['r1']))){            
            echo "This Line Prints";
                echo "This Line also Prints";

The two statements mentioned in the above code are printing but the query_posts is not running here. However when I am running it outside the session it is working fine :S How can I run this query_posts if the Session is empty? Why doesn't this code work?

share|improve this question
You're calling wp_reset_query() right after you run query_posts(); so how do you know that query_posts() isn't running, and simply being reset? – Chip Bennett Feb 8 '12 at 17:29
I've already checked that without calling wp_reset_query() & it doesn't work either – Sufiyan Ghori Feb 8 '12 at 17:35
where are you setting $id? also- remove the dot and single quotes around $id. – Milo Feb 8 '12 at 17:55
this is not the issue either, as if i replace $id with the constant category ID it doesn't work either , btw $id is declared right above this code – Sufiyan Ghori Feb 8 '12 at 17:59
We need to see actual code that you're using, not test code. – Chip Bennett Feb 8 '12 at 18:05
up vote 2 down vote accepted
if ( empty($_SESSION['r1']) ) {
    global $query_string;         
    query_posts($query_string . "&cat=-" . $id);

This should work, depending on $id is set.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.