Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Odd php / wordpress problem (global variables)

Options
  • 19-05-2011 12:53pm
    #1
    Registered Users Posts: 1,657 ✭✭✭


    I have an odd problem... I'm trying to make it think it's on a search page

    so I have code like this.
    (code to get the posts)
    
    global $wp_query;
    
    $wp_query->is_search = true;
    
    //more code here... 
    //etc.
    //etc.
    
    echo $wp_query->is_search; //displays 1
    
    if (is_search()) {
      echo "Search results : blah blah";
    } else {
      echo "Not on search page";
    }
    
    

    And is_search () is defined as
    function is_search() {
     global $wp_query;
    
     return $wp_query->is_search;
    }
    

    Now wp_query contains all the info on the posts. But for some reason, it's got completely different values inside that function than it does outside it. It refers to different posts (the home page, I think) Hence, is_search() is returning false.

    I've checked immeadiately before the call to is_search so it's not being changed by the intervening code.

    'story?


Comments

  • Registered Users Posts: 1,657 ✭✭✭komodosp


    Just to expand on this -
    To get the data to put in $wp_query I'm calling query_posts().
    But there is data in $wp_query before I call query_posts(). That gets replaced with the data I want.

    However, when we get into is_search(), the value in $wp_query reverts to how it was before I called query_posts().


  • Registered Users Posts: 1,657 ✭✭✭komodosp


    Not to worry, I've gotten around it by programming a completely different way... Still a curious thing though.


Advertisement