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?

  • 1
    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
if ( empty($_SESSION['r1']) ) {
    global $query_string;         
    query_posts($query_string . "&cat=-" . $id);

This should work, depending on $id is set.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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